Streaming systems divide unbounded event sequences into finite windows before computing aggregates. A tumbling window uses fixed-size, non-overlapping intervals, so each event belongs to exactly one window.
Design a TumblingWindowAggregator class:
TumblingWindowAggregator() creates a stateless aggregator.int[] aggregate(int[] timestamps, int[] values, int windowSize) returns the sum of values in each touched range of the timeline.
The event at timestamps[i] has value values[i]. Windows begin at timestamp 0, and the event at time t belongs to window index:
where / is integer division. Return one sum for every window from index 0 through the highest window touched by any event. If no event belongs to a window inside that range, its sum must be 0.
Timestamps do not have to be sorted, and values may be negative.
Example 1:
Input:
Output:
Explanation: Window 0 covers timestamps 0 through 4 and sums 10 + 20 = 30. Window 1 covers timestamps 5 through 9 and sums 30 + 40 = 70.
Example 2:
Input:
Output:
Explanation: The events touch windows 0 and 2. Window 1 contains no events but must remain in the output with a sum of 0.
Constraints
1 <= timestamps.length <= 1000timestamps.length == values.length0 <= timestamps[i] <= 10^5-10^4 <= values[i] <= 10^41 <= windowSize <= 10^5- Every window sum fits in a signed 32-bit integer.
- At most
100 calls are made to aggregate.