Skip to main content

Calculate Money in Leetcode Bank - Solution & Explanation

EasyMath16 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

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 <= 1000

Approach Overview

Problem 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 1: Iterative Approach

The iterative approach involves simulating each day in a loop, tracking both the weeks and individual days. We'll add the appropriate amount of money for each day based on the rules provided. This approach leverages basic looping constructs and conditionally increments the sums based on the day of the week.

This C solution uses a for loop to iterate through each day until n. It calculates the current 'week' number and the 'day of the week' using modulo arithmetic. It adds the appropriate amount for that day to a running total.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of days. Each day is processed once.
Space Complexity: O(1), only a constant amount of space is used.

Try this approach in the editor →

Approach 2: Mathematical Approach

The mathematical approach seeks to minimize iterations by using formulae to directly compute sums based on weeks, leveraging arithmetic series properties. This method is typically more efficient and concise.

This solution calculates the contribution of complete weeks and the remaining days separately using arithmetic sum formulae. It computes the sum of weeks' contributions and adds the extra days with incremental daily increases.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1).
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Math

According to the problem description, the deposit situation for each week is as follows:

Week 1: 1, 2, 3, 4, 5, 6, 7
Week 2: 2, 3, 4, 5, 6, 7, 8
Week 3: 3, 4, 5, 6, 7, 8, 9
...
Week k: k, k+1, k+2, k+3, k+4, k+5, k+6

Given n days of deposits, the number of complete weeks is k = \lfloor n / 7 \rfloor, and the remaining days is b = n \mod 7.

The total deposit for the complete k weeks can be calculated using the arithmetic sequence sum formula:

$ S_1 = \frac{k}{2} times (28 + 28 + 7 times (k - 1))

The total deposit for the remaining b days can also be calculated using the arithmetic sequence sum formula:

S_2 = \frac{b}{2} times (k + 1 + k + 1 + b - 1)

The final total deposit amount is S = S_1 + S_2.

The time complexity is O(1) and the space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterative Approach

Time Complexity: O(n), where n is the number of days. Each day is processed once.
Space Complexity: O(1), only a constant amount of space is used.

Mathematical Approach

Time Complexity: O(1).
Space Complexity: O(1).

Math—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative SimulationO(n)O(1)Best for clarity and quick implementation when constraints are small
Mathematical FormulaO(1)O(1)Preferred in interviews to demonstrate pattern recognition and arithmetic series optimization

Video Solution

Calculate Money in Leetcode Bank - Leetcode 1716 - Python • NeetCodeIO • 8,637 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Calculate Money in Leetcode Bank easy or hard?
Calculate Money in Leetcode Bank is classified as an Easy problem on LeetCode with an acceptance rate above 80%. It mainly tests basic iteration, arithmetic patterns, and simple mathematical optimization.
Calculate Money in Leetcode Bank Python/Java solution
Both Python and Java implementations follow the same logic. You can either simulate deposits day by day using a loop or compute the result directly using the arithmetic formula for weekly sums. The mathematical approach runs in O(1) time and is typically the preferred implementation.
How to solve Calculate Money in Leetcode Bank in O(1)?
First compute full weeks w = n / 7 and remaining days r = n % 7. Each week forms an arithmetic sequence where the starting value increases by one. Sum all full weeks using arithmetic series formulas, then add the deposits for the remaining r days starting from w + 1. This reduces the problem to constant-time arithmetic operations.
What is the best approach for Calculate Money in Leetcode Bank?
The mathematical formula approach is the best solution. It computes the total savings in O(1) time by calculating the sum of full weeks using an arithmetic progression and then adding the remaining days. This avoids iterating through all n days and demonstrates pattern recognition.
Is Calculate Money in Leetcode Bank asked at Google/Amazon/Meta?
This problem is categorized as Easy and mainly tests basic math and pattern recognition. While it is less common in top-tier interview rounds, similar arithmetic pattern problems appear in coding screens and practice sets used by companies like Amazon and Google.
What data structure is used in Calculate Money in Leetcode Bank?
No specialized data structure is required. The problem is solved using simple integer variables and arithmetic calculations. It mainly relies on mathematical reasoning and optional loop-based simulation.
What is the time complexity of Calculate Money in Leetcode Bank?
Two common solutions exist. The simulation approach runs in O(n) time by iterating through each day and adding the deposit. The optimized mathematical approach runs in O(1) time because it directly computes the sum using arithmetic formulas for weekly deposits.

Ready to solve this problem?

Practice Calculate Money in Leetcode Bank with our built-in code editor and test cases.

Practice on FleetCode