Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
Deposits increase by one within a week, but each new Monday starts one higher than the previous Monday. The bounded input allows the reference solutions to precompute the whole schedule once and answer by summing its first n days.
Approach
Initialize day zero's deposit to one. For later precomputed days, use the previous day's amount plus one unless the day begins a new week; then use the amount seven days earlier plus one. Store 1001 schedule entries. For each query, slice the first n entries and sum them. This follows the stored implementation's schedule table, rather than an arithmetic-series formula or prefix-sum lookup.
arr = [1]
for i in range(1, 1001):
if i % 7 == 0: curr = arr[-7] + 1
else: curr = arr[-1] + 1
arr.append(curr)
class Solution:
def totalMoney(self, n: int) -> int:
return sum(arr[:n])Complexity analysis
Let U = 1001 be the fixed precomputed schedule size and n the query length. Module initialization takes O(U) time and O(U) persistent space. totalMoney(n) takes O(n) time and O(n) temporary space because the displayed code slices the first n entries before summing.
