Calculate Money in Leetcode Bank: Weekly Deposit Pattern
Hercy deposits money into a bank following a strict weekly pattern. On Monday of the first week he deposits $1. Each subsequent day he deposits $1 more than the day before. On the next Monday he starts $1 higher than the previous Monday, and the daily increments continue. Given n days, return the total amount deposited.
This is LeetCode 1716: Calculate Money in Leetcode Bank, an easy problem that rewards you for recognizing arithmetic series hiding inside a weekly cycle.
Why this problem matters
Problems built on repeating numeric patterns are everywhere in real software. Billing cycles, subscription tiers, progressive pricing schedules, and batch processing quotas all follow the same idea: a base amount that changes predictably over fixed intervals. Once you can decompose a sequence into complete periods plus a remainder, you can compute totals in constant time instead of iterating day by day.
Calculate Money in Leetcode Bank is a clean example of that decomposition. The weekly structure gives you a natural "period," and the increasing start value across weeks forms an arithmetic series. Recognizing those two layers is the entire problem.
The key insight
Each week is its own arithmetic series. Week 1 deposits 1, 2, 3, 4, 5, 6, 7. Week 2 deposits 2, 3, 4, 5, 6, 7, 8. In general, week k (1-indexed) deposits k, k+1, k+2, ..., k+6. The sum of week k is 7k + 21, because you add seven copies of k plus the fixed offset 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21.
If n is not a perfect multiple of 7, the last partial week starts at full_weeks + 1 and increments by 1 for each remaining day. You sum that partial arithmetic series separately.
The algorithm:
- Calculate the number of complete weeks and remaining days.
- Sum each complete week using the formula for an arithmetic series.
- Add the remaining days from the partial week.
The solution
def total_money(n: int) -> int:
weeks, days = divmod(n, 7)
total = 0
for w in range(weeks):
total += 7 * (w + 1) + 21
for d in range(days):
total += weeks + 1 + d
return total
The first loop handles complete weeks. For week w (0-indexed), the sum is 7 * (w + 1) + 21. That comes from adding (w+1) + (w+2) + ... + (w+7), which equals 7(w+1) + 21.
The second loop handles the leftover days. The partial week starts on a Monday with a deposit of weeks + 1 (since this is week number weeks + 1 in the overall sequence). Each subsequent day adds $1 more.
You can also collapse both loops into closed-form math. The sum of 7k + 21 for k from 1 to W is 7 * W * (W + 1) / 2 + 21 * W. The remaining days form a small arithmetic series starting at W + 1 with length r. Either approach gives you the same answer. The loop version is clearer to read, and the closed-form version runs in O(1).
The sum of an arithmetic series with first term a, last term b, and count c is c * (a + b) / 2. You can apply this formula to both the complete weeks and the remaining days to get an O(1) solution with no loops at all.
Visual walkthrough
Let's trace through n = 10. There is 1 complete week (7 days) and 3 remaining days. Week 1 sums to 28, and the 3 leftover days from week 2 deposit $2 + $3 + $4 = 9. The total is 37.
Step 1: Split into complete weeks and remaining days
n = 10. We have 10 // 7 = 1 complete week and 10 % 7 = 3 remaining days.
Step 2: Sum the complete week
Week 1: 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
Formula: 7k + 21 where k = 1 gives 7(1) + 21 = 28
Week 1 deposits $1 through $7. The sum is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
Step 3: Sum the remaining days
Remaining days start at: full_weeks + 1 = 2
Deposits: $2 + $3 + $4 = 9
The 3 leftover days are the start of week 2. They deposit $2, $3, $4.
Step 4: Combine for the final answer
total = weeks_total + remaining_total
total = 28 + 9 = 37
Total = 28 + 9 = 37. For n = 10, the answer is 37.
Each step builds on the previous one. First you identify how the days split into weeks and remainders. Then you compute each piece using the arithmetic series formula. Finally you add them together.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Week-by-week sum | O(n/7) | O(1) |
| Closed-form math | O(1) | O(1) |
Time: The loop-based approach iterates once per complete week, so it runs in O(n/7) which simplifies to O(n). The closed-form approach uses a constant number of arithmetic operations regardless of input size.
Space: Both approaches use a fixed number of variables. No arrays or data structures are needed.
The building blocks
1. Arithmetic series summation
The sum of consecutive integers from a to b is (b - a + 1) * (a + b) / 2. This formula appears constantly in math-oriented problems. Here, each week's deposits form a consecutive range, and the week sums themselves form another arithmetic progression.
def arithmetic_sum(a, b):
count = b - a + 1
return count * (a + b) // 2
This is the same building block used in problems like Arranging Coins and Sum of First N Even Numbers. Once you can recognize when a sequence is arithmetic, you can skip the loop entirely.
2. Week/day decomposition
Splitting a total count into complete periods and a remainder is a pattern that shows up in cyclic problems. The divmod function gives you both pieces in one call.
weeks, days = divmod(n, 7)
This decomposition separates the problem into two independent subproblems: one for complete cycles and one for the leftover. You will see the same technique in problems involving circular arrays, modular arithmetic, and calendar calculations.
Edge cases
- n = 1: Only one day. Deposit $1. Return 1.
- n = 7: Exactly one full week. Return 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
- n is a multiple of 7: No remainder days. The total comes entirely from complete weeks.
- Large n: The closed-form formula handles any value of n instantly. Even the loop version only iterates n/7 times, which is fast for the constraint
nup to 1000.
From understanding to recall
You can read through this solution and follow the arithmetic without trouble. But the real challenge is reproducing the decomposition from scratch when you see a similar problem in an interview. Which formula was it? Does the remainder start at weeks or weeks + 1? These details slip away quickly if you only see the problem once.
Spaced repetition fixes that. CodeBricks breaks this problem into its building blocks (arithmetic series summation, period-remainder decomposition) and drills them at increasing intervals. Each time you reconstruct the formula from memory, the pattern gets more automatic. After a few review cycles, you reach for divmod(n, 7) and the series sum without hesitation.
Related posts
- Climbing Stairs - Another pattern-based math problem where recognizing the recurrence is the key
- Add Digits - A math problem where the closed-form formula eliminates the need for iteration
- Power of Three - Pattern recognition in number properties
The weekly deposit pattern in this problem is a clean example of how arithmetic series appear in disguise. Once you internalize the building blocks of series summation and cycle decomposition, you can solve an entire family of problems without writing a single loop. CodeBricks helps you drill these building blocks until they become second nature.