Delta / Difference Array
The idea of prefix sum is to construct an array that record the changes happened at each index. This is often useful when deal with scheduling issue and when working with a time series of events
Schedule - bookings
|-----------------------| [0, 6]
|-----------| [2, 5]
|-------| [1, 3]
|-------------------| [2, 7]
+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8
Delta / Diff Array
1 1 2 0 -1 0 -1 -1 -1
+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8
Number of booking at each timestamp
1 2 4 4 3 3 2 1 0
+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8
Example
In the graph above we have the following 4 time slot [0, 6], [2, 5], [1, 3], [4, 7]
, let's say that we want to find out at each timestamp how many booking are there on the schedule. How can we figure it out?
Using a BTreeMap
Using a Vec