Calculate Money in Leetcode Bank (LeetCode 1716): Solution and Explanation

Easy
Math

Hercy deposits $1 on the first Monday and $1 more each day through Sunday. Every new Monday starts $1 higher than the previous Monday. Return the total saved after n days.

Intuition

Lay the deposits out as a calendar with one row per week. Row 1 reads 1, 2, ..., 7. Row 2 reads 2, 3, ..., 8. Every row is the previous row shifted up by 1 in each of its seven cells.

That makes each full row 7 more than the row above it. Row 1 sums to 28, row 2 to 35, row 3 to 42, and in general week k sums to 7k + 21. The full weeks together form an arithmetic series.

Whatever is left after the full weeks is a short partial row. It starts at weeks + 1 on Monday and climbs by 1 a day for days days, which is another small arithmetic series.

With weeks, days = divmod(n, 7), both parts have closed forms, so the whole answer comes from a few multiplications. A day-by-day loop also works for n up to 1000, but the formula shows the structure.

Approach: Two arithmetic series

  1. Split the days: weeks = n // 7, days = n % 7.
  2. Full weeks sum to 28 + 35 + ... = 28 * weeks + 7 * (0 + 1 + ... + (weeks - 1)) = 28 * weeks + 7 * weeks * (weeks - 1) / 2.
  3. Leftover days deposit weeks + 1, weeks + 2, ..., weeks + days, which sums to days * (weeks + 1) + days * (days - 1) / 2.
  4. Return the two parts added together.

Step-by-step example

n = 10
  1. divmod(10, 7) gives weeks = 1 and days = 3.
  2. Full weeks: 28 * 1 + 7 * 1 * 0 / 2 = 28, the first row 1 + 2 + ... + 7.
  3. The second Monday starts at weeks + 1 = 2. Leftover days: 3 * 2 + 3 * 2 / 2 = 6 + 3 = 9, which is 2 + 3 + 4.
  4. Total = 28 + 9 = 37.

Code: Python, Java, C++

1class Solution:
2    def totalMoney(self, n: int) -> int:
3        weeks, days = divmod(n, 7)
4        full = 28 * weeks + 7 * weeks * (weeks - 1) // 2
5        partial = days * (weeks + 1) + days * (days - 1) // 2
6        return full + partial
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

A fixed number of arithmetic operations no matter how large n is.

Space

O(1)

Only a few integers are stored.

Other approaches

Simulate each day (time O(n), space O(1))

Loop i from 0 to n - 1 and add i // 7 + 1 + i % 7, the Monday base of the week plus the day offset. It is easy to get right and fast enough for n up to 1000.

Loop over weeks (time O(n / 7), space O(1))

Add 28 + 7w for each full week w, then loop over the leftover days. It is a middle ground that makes the weekly pattern explicit without the closed form.

Common mistakes

  • Starting the leftover days at 1 instead of weeks + 1. The partial week's Monday is one higher than the last full week's Monday.
  • Using weeks * (weeks + 1) / 2 instead of weeks * (weeks - 1) / 2 for the extra 7 per week. The first week adds nothing extra.
  • Dividing before multiplying in integer arithmetic, which can drop a half. 7 * weeks * (weeks - 1) / 2 is exact because weeks * (weeks - 1) is always even.
  • Forgetting the case where n is a multiple of 7, where the leftover part must be 0.

Frequently asked questions

Why does each full week sum to 7 more than the previous one?

Every day of the new week deposits exactly $1 more than the same weekday of the previous week, and there are seven days, so the weekly total grows by 7.

Is a simple loop acceptable?

Yes. With n at most 1000 a day-by-day loop runs instantly. The closed form is a nice follow-up and shows you can spot arithmetic series.

Can the answer overflow a 32-bit integer?

No. For n = 1000 the total is 74,926, far below the 32-bit limit.

What is the time complexity?

O(1) with the formula, or O(n) with a simple loop. Both use O(1) space.