1109. Corporate Flight Bookings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int[] corpFlightBookings(int[][] bookings, int n) {
int[] diff = new int[n];

for (int[] book : bookings) {
int from = book[0] - 1, to = book[1] - 1, count = book[2]; // [from, to]
diff[from] += count;
if (to + 1 < diff.length) {
diff[to + 1] -= count;
}
}

int[] res = new int[n];
res[0] = diff[0];
for (int i = 1; i < res.length; i++) {
res[i] = res[i - 1] + diff[i];
}
return res;
}
}

References

1109. Corporate Flight Bookings