File: calculate-money-in-leetcode-bank/solution.py

Date: 2026-06-06

Time: 15:30

calculate-money-in-leetcode-bank/solution.py

Purpose

Solves LeetCode 1716. The problem models a weekly savings pattern: on day 1 (Monday) of week 1 you deposit $1, incrementing by $1 each day through Sunday ($7). Each subsequent Monday starts $1 higher than the previous Monday. Given n days, return the total deposited.

Key Components

Solution.totalMoney(n: int) -> int — the single method. It computes the answer in O(1) using closed-form arithmetic rather than simulating day by day.

Flow

The method decomposes n into complete weeks and a remainder, then sums each part with arithmetic series formulas:

1. fullweeks = n // 7 and remainingdays = n % 7 — split the timeline.

2. Complete weeks sum — Week k (0-indexed) deposits values (k+1), (k+2), ..., (k+7), totaling 28 + 7k. Summing over all complete weeks:

weekstotal = fullweeks * 28 + 7 * fullweeks * (fullweeks - 1) // 2

This is Σ(28 + 7k) for k = 0..full_weeks-1, which expands to 28W + 7·W(W-1)/2.

3. Remaining days sum — The partial week's Monday starts at deposit value fullweeks + 1. The remaining days deposit (fullweeks+1), (fullweeks+2), ..., (fullweeks+remaining_days):

daystotal = remainingdays * (fullweeks + 1) + remainingdays * (remaining_days - 1) // 2

This is the standard sum of an arithmetic sequence starting at fullweeks + 1 with remainingdays terms.

4. Return weekstotal + daystotal.

Patterns

Dependencies

Invariants

Error Handling

None. The method trusts its input matches the LeetCode contract. Passing n = 0 would return 0 (correct by convention). Negative n would produce a nonsensical but non-crashing result due to Python's floor-division semantics.