Given a list of Unix timestamps (integers in seconds) sorted in non-decreasing order, group them into consecutive 7-day intervals.
The first interval begins at the first timestamp and spans the next 7 days (604800 seconds). The second interval begins exactly 7 days after the first, and so on. Each timestamp belongs to the interval [start, start + 604800) that contains it. Intervals with no timestamps are represented as empty lists, so that the i-th inner list always corresponds to the i-th consecutive 7-day window starting from the first timestamp.
Return a list of lists, where each inner list contains the timestamps falling within that 7-day window, preserving their input order.
Function Signature
def weekly_aggregation(timestamps: list[int]) -> list[list[int]]:
Rules
-
Let
first = timestamps[0]
. Window
i
(counting from 0) is the half-open interval
[first + i * 604800, first + (i + 1) * 604800)
.
-
The result has one list per window, from window 0 through the window that contains the last timestamp, so its length is
(timestamps[-1] - first) // 604800 + 1
. Empty windows between them appear as
[]
, and the last list is never empty.
-
Equal timestamps are all kept, in input order.
Constraints
-
1 <= len(timestamps) <= 10^5
-
0 <= timestamps[i] <= 2 * 10^9
-
timestamps
is sorted in non-decreasing order.
Examples
Input: timestamps = [1700000000, 1700100000, 1700500000, 1700700000, 1701300000, 1702000000]
Output: [[1700000000, 1700100000, 1700500000], [1700700000], [1701300000], [1702000000]]
Window 1 is [1700000000, 1700604800) and holds the first three timestamps. Windows 2, 3 and 4 are [1700604800, 1701209600), [1701209600, 1701814400) and [1701814400, 1702419200), and each holds one timestamp.
Input: timestamps = [0, 100, 604799, 604800, 1209599, 1209600, 5000000]
Output: [[0, 100, 604799], [604800, 1209599], [1209600], [], [], [], [], [], [5000000]]
604799 is the last second of window 1 and 604800 is the first second of window 2. Windows 4 through 8 contain nothing, and 5000000 falls in window 9, [4838400, 5443200), so the result has nine lists.
Input: timestamps = [5, 5, 604805]
Output: [[5, 5], [604805]]
Windows are measured from the first timestamp, so window 1 is [5, 604805). Both copies of 5 stay in it, and 604805 is exactly 604800 seconds later, so it opens window 2.