Aggregate Time Series
MediumTwo Pointers · Uber
first and second are time series, each a list of [timestamp, value] pairs sorted by strictly increasing timestamp.
Build their sum at every timestamp that appears in either series. For a timestamp t, a series contributes:
- its own value at
t, if it has a point att; - otherwise the value of its next point, meaning its first point with a timestamp greater than
t; - otherwise (no later point exists)
0.
Return a list of [timestamp, summed_value] pairs in increasing timestamp order, with each timestamp appearing once.
Example 1
- Input:
- first = [[2, 4], [5, 1], [9, 6]], second = [[1, 3], [5, 2], [7, 8]]
- Output:
- [[1, 7], [2, 6], [5, 3], [7, 14], [9, 6]]
- Explanation
- At
t = 1,firsthas no point, so it borrows 4 from its next point at 2, giving 3 + 4 = 7. Att = 7,firstborrows 6 from time 9. Att = 9,secondhas nothing later, so it adds 0 and the sum is 6.
Example 2
- Input:
- first = [[1, 10], [3, 20]], second = [[1, 5], [3, 5]]
- Output:
- [[1, 15], [3, 25]]
- Explanation
- Both series have points at 1 and 3, so the values add directly: 15 and 25.
Example 3
- Input:
- first = [[4, 7]], second = []
- Output:
- [[4, 7]]
- Explanation
- The empty series has no next point anywhere, so it always contributes 0.
Constraints
0 <= first.length, second.length <= 10^5- Timestamps within each series are strictly increasing integers,
-10^9 <= timestamp <= 10^9 0 <= value <= 10^6