Apply many range updates in O(1) each and read the result in one pass.
change[i] is the difference between place i and place i - 1, so the running total up to place i is its value.
Use it when many range updates come first and the values are read after all of them. Signal words: add to every place in a range, many updates, how many are active, overlapping windows.
Start: a change list of n + 1 zeros.
Step: for an update from left to right with an amount, add the amount at left and subtract it at right + 1.
Rebuild: one running total over the change list gives every value. For huge coordinates, sort the events and sweep.
It does not fit reads that come between updates, or an update that is not an addition.
Cost: O(1) for each update and O(n) to rebuild, with O(n) extra space.
def range_updates(size, updates):
# [1] Make a change list one longer than the array, filled with zeros.
change = [0] * (size + 1)
# [2] Record each update in two places: add it where it starts and take it away right after it ends.
for left, right, amount in updates:
change[left] += amount
change[right + 1] -= amount
# [3] Walk the change list with a running total: the total at each place is the result there.
result = []
running = 0
for index in range(size):
running += change[index]
result.append(running)
# [4] Return the array after all updates.
return resultpublic long[] rangeUpdates(int size, int[][] updates) {
// [1] Make a change list one longer than the array, filled with zeros.
long[] change = new long[size + 1];
// [2] Record each update in two places: add it where it starts and take it away right after it ends.
for (int[] update : updates) {
change[update[0]] += update[2];
change[update[1] + 1] -= update[2];
}
// [3] Walk the change list with a running total: the total at each place is the result there.
long[] result = new long[size];
long running = 0;
for (int index = 0; index < size; index++) {
running += change[index];
result[index] = running;
}
// [4] Return the array after all updates.
return result;
}vector<long long> rangeUpdates(int size, vector<vector<int>>& updates) {
// [1] Make a change list one longer than the array, filled with zeros.
vector<long long> change(size + 1, 0);
// [2] Record each update in two places: add it where it starts and take it away right after it ends.
for (auto& update : updates) {
change[update[0]] += update[2];
change[update[1] + 1] -= update[2];
}
// [3] Walk the change list with a running total: the total at each place is the result there.
vector<long long> result;
long long running = 0;
for (int index = 0; index < size; index++) {
running += change[index];
result.push_back(running);
}
// [4] Return the array after all updates.
return result;
}function rangeUpdates(size, updates) {
// [1] Make a change list one longer than the array, filled with zeros.
const change = new Array(size + 1).fill(0);
// [2] Record each update in two places: add it where it starts and take it away right after it ends.
for (const [left, right, amount] of updates) {
change[left] += amount;
change[right + 1] -= amount;
}
// [3] Walk the change list with a running total: the total at each place is the result there.
const result = [];
let running = 0;
for (let index = 0; index < size; index++) {
running += change[index];
result.push(running);
}
// [4] Return the array after all updates.
return result;
}def max_overlap(intervals):
# [1] Turn each window into two events: +1 at its start and -1 at its end.
events = []
for start, end in intervals:
events.append((start, 1))
events.append((end, -1))
# [2] Sort the events by time; at the same time an end comes before a start, because the windows are half open.
events.sort()
# [3] Sweep through the events with a running count, and keep the highest count seen.
running = 0
best = 0
for time, step in events:
running += step
best = max(best, running)
# [4] Return the highest count.
return bestpublic int maxOverlap(int[][] intervals) {
// [1] Turn each window into two events: +1 at its start and -1 at its end.
long[] events = new long[intervals.length * 2];
for (int i = 0; i < intervals.length; i++) {
events[2 * i] = (long) intervals[i][0] * 2 + 1;
events[2 * i + 1] = (long) intervals[i][1] * 2;
}
// [2] Sort the events by time; at the same time an end comes before a start, because the windows are half open.
java.util.Arrays.sort(events);
// [3] Sweep through the events with a running count, and keep the highest count seen.
int running = 0;
int best = 0;
for (long event : events) {
running += (event % 2 == 1) ? 1 : -1;
best = Math.max(best, running);
}
// [4] Return the highest count.
return best;
}int maxOverlap(vector<vector<int>>& intervals) {
// [1] Turn each window into two events: +1 at its start and -1 at its end.
vector<pair<int, int>> events;
for (auto& window : intervals) {
events.push_back({window[0], 1});
events.push_back({window[1], -1});
}
// [2] Sort the events by time; at the same time an end comes before a start, because the windows are half open.
sort(events.begin(), events.end());
// [3] Sweep through the events with a running count, and keep the highest count seen.
int running = 0;
int best = 0;
for (auto& event : events) {
running += event.second;
best = max(best, running);
}
// [4] Return the highest count.
return best;
}function maxOverlap(intervals) {
// [1] Turn each window into two events: +1 at its start and -1 at its end.
const events = [];
for (const [start, end] of intervals) {
events.push([start, 1]);
events.push([end, -1]);
}
// [2] Sort the events by time; at the same time an end comes before a start, because the windows are half open.
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
// [3] Sweep through the events with a running count, and keep the highest count seen.
let running = 0;
let best = 0;
for (const [time, step] of events) {
running += step;
best = Math.max(best, running);
}
// [4] Return the highest count.
return best;
}This version writes the take-away at right instead of right + 1.
This version makes the change list the same length as the array. An update that ends at the last place writes to place n.
This version returns the change list itself and skips the running total.
This version sorts events so that a start comes before an end at the same time. Half-open windows that touch would then overlap.
The support desk now changes the plan all day and reads the number of agents online right after each change. Each read needs the running total, so each read needs a rebuild.
systemdesignmastery.dev/dsa/cheatsheets/difference-arrays