Watch 10 video solutions for Calculate Money in Leetcode Bank, a easy level problem involving Math. This walkthrough by NeetCodeIO has 8,521 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.
He starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, he will put in $1 more than the day before. On every subsequent Monday, he will put in $1 more than the previous Monday.
Given n, return the total amount of money he will have in the Leetcode bank at the end of the nth day.
Example 1:
Input: n = 4 Output: 10 Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
Example 2:
Input: n = 10 Output: 37 Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
Example 3:
Input: n = 20 Output: 96 Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
Constraints:
1 <= n <= 1000Problem Overview: You deposit money into the LeetCode bank every day following a pattern. On Monday you start with $1, Tuesday $2, ... Sunday $7. Each new week starts one dollar higher than the previous Monday. Given n days, compute the total amount saved.
Approach 1: Iterative Simulation (O(n) time, O(1) space)
The most direct solution simulates the deposit process day by day. Track the current week's starting value and increment the daily deposit as the week progresses. Every time seven days pass, increase the starting value for the next week. You iterate from day 1 to n, add the appropriate deposit for each day, and accumulate the total. This approach mirrors the problem statement exactly and is easy to implement using simple arithmetic and a loop.
This method works well because the constraints are small enough that iterating through n days is trivial. The logic typically tracks two values: the starting deposit for the week and the day offset inside the week. Concepts like basic iteration and counters make this a good introductory problem for simulation and simple math reasoning.
Approach 2: Mathematical Formula (O(1) time, O(1) space)
The optimal solution avoids iterating through each day by recognizing a repeating weekly structure. Every week contains an arithmetic sequence of seven deposits. If n days are given, first compute the number of full weeks w = n / 7 and remaining days r = n % 7. The total contribution of full weeks forms a pattern where each week's starting value increases by one.
The weekly deposits themselves follow an arithmetic progression: week 1 sums to 28 (1 through 7), week 2 sums to 35 (2 through 8), and so on. The total for all full weeks can be derived using arithmetic series formulas. After computing that value, add the contribution from the remaining r days starting from w + 1. This converts a day-by-day simulation into a few constant-time calculations using properties of mathematical series.
Recommended for interviews: Start with the iterative simulation to show you understand the deposit pattern and can translate the rules into code. Then optimize with the mathematical formula. Interviewers often expect the O(1) solution because it demonstrates pattern recognition and comfort with arithmetic series instead of relying only on loops.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Simulation | O(n) | O(1) | Best for clarity and quick implementation when constraints are small |
| Mathematical Formula | O(1) | O(1) | Preferred in interviews to demonstrate pattern recognition and arithmetic series optimization |