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.
Example 1:
Input: series1 = [[1,3],[4,1]], series2 = [[2,2],[5,2]]
Output: [[1,5],[2,3],[4,3],[5,2]]
Explanation:
| Timestamp | series1 |
series2 |
summedValue |
|---|---|---|---|
| 1 | 3 | 2 | 5 |
| 2 | 1 | 2 | 3 |
| 4 | 1 | 2 | 3 |
| 5 | 0 | 2 | 2 |
Thus, the aggregated series is [[1, 5], [2, 3], [4, 3], [5, 2]].
Example 2:
Input: series1 = [[1,5],[3,1]], series2 = [[2,2]]
Output: [[1,7],[2,3],[3,1]]
Explanation:
| Timestamp | series1 |
series2 |
summedValue |
|---|---|---|---|
| 1 | 5 | 2 | 7 |
| 2 | 1 | 2 | 3 |
| 3 | 1 | 0 | 1 |
Thus, the aggregated series is [[1, 7], [2, 3], [3, 1]].
Example 3:
Input: series1 = [[1,5]], series2 = [[1000000000,2]]
Output: [[1,7],[1000000000,2]]
Explanation:
At timestamp 1, the next available value in series2 is 2 at timestamp 1000000000. At timestamp 1000000000, there is no later timestamp in series1, so its value is 0. Only timestamps that appear in at least one of the two series are included.
Constraints:
1 <= series1.length, series2.length <= 105series1[i].length == series2[i].length == 21 <= series1[i][0], series2[i][0] <= 1091 <= series1[i][1], series2[i][1] <= 109timestamp.Problem Overview: You are given two time series datasets where each entry contains a timestamp and a value. The goal is to combine both series into a single aggregated result by timestamp. Matching timestamps should have their values summed, while unique timestamps should still appear in the final output.
Approach 1: Brute Force Comparison (Time: O(n * m), Space: O(1))
The direct solution compares every entry in the first series against every entry in the second series. For each timestamp match, you aggregate the values and mark the pair as processed. After the nested iteration finishes, append the remaining unmatched timestamps. This approach works for small datasets and helps verify correctness before optimizing, but repeated scans make it inefficient for large inputs.
Approach 2: Hash Map Aggregation (Time: O(n + m), Space: O(n + m))
Use a hash map where the key is the timestamp and the value is the running aggregate. Iterate through the first series and insert values into the map. Then iterate through the second series and update existing timestamps or create new entries. After processing both arrays, convert the map into the required output format and sort by timestamp if needed. This is the standard solution for unsorted input because hash lookup runs in constant average time. Problems involving hash tables and arrays frequently use this pattern.
Approach 3: Two Pointers on Sorted Series (Time: O(n + m), Space: O(1) extra)
If both time series are already sorted by timestamp, two pointers produce the cleanest solution. Start one pointer at the beginning of each series. Compare timestamps: when they match, append the summed value and move both pointers; otherwise append the smaller timestamp and move the corresponding pointer. After one series finishes, append the remaining elements from the other series. This avoids hash table overhead and preserves sorted order naturally. The same technique appears in two pointers and merge-style interval problems.
Recommended for interviews: Interviewers typically expect the hash map or two-pointer approach depending on whether the input is sorted. The brute force version demonstrates baseline reasoning, but the optimized O(n + m) solution shows that you recognize aggregation patterns and understand how to reduce repeated scans. If sorted order is guaranteed, two pointers are usually preferred because they achieve linear time with minimal extra memory.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Comparison | O(n * m) | O(1) | Small datasets or validating correctness first |
| Hash Map Aggregation | O(n + m) | O(n + m) | General case with unsorted timestamps |
| Two Pointers on Sorted Series | O(n + m) | O(1) | When both series are already sorted |
Practice Aggregate Two Time Series with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor