[LeetCode] Aggregate Two Time Series

4001. Aggregate Two Time Series

You are given two 2D integer arrays series1 and series2.

Each element in both series is of the form [timestamp, value], where:

  • timestamp is an integer representing the time.
  • value is an integer representing the value at that timestamp.

Each array is sorted in strictly increasing order of timestamp.

For any timestamp not present in a series, its value is taken from the next available timestamp in the same series if one exists. Otherwise, its value is considered 0.

The aggregated series is formed by summing the corresponding values from both series at every timestamp that appears in either series.

Return the aggregated series as a 2D integer array of [timestamp, summedValue] pairs, sorted in strictly increasing order of timestamp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> aggregateTimeSeries(vector<vector<int>>& series1, vector<vector<int>>& series2) {
reverse(begin(series1), end(series1));
reverse(begin(series2), end(series2));
vector<vector<int>> res;
while(series1.size() or series2.size()) {
int t = INT_MAX, c = 0;
if(series1.size()) {
t = min(t, series1.back()[0]);
c += series1.back()[1];
}
if(series2.size()) {
t = min(t, series2.back()[0]);
c += series2.back()[1];
}
res.push_back({t,c});
if(series1.size() and series1.back()[0] == t) series1.pop_back();
if(series2.size() and series2.back()[0] == t) series2.pop_back();
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/aggregate-two-time-series/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.