Talentd
FleetCode
Make and follow sheets.Sign in

Calculate Money in Leetcode Bank solution and explanation

EasyMath6 min read82.4% acceptanceUpdated 9 Apr 2026
Calculate Money in Leetcode Bank - Leetcode 1716 - PythonNeetCodeIO · 14:34 · 8K viewsWatch on YouTube
9 more videos on this problem
  1. Calculate Money in Leetcode Bank - Leetcode 1716 - PythonNeetCodeIO · 14:34 · 8K views
  2. Calculate Money in Leetcode Bank | 3 Easy Approaches | Leetcode 1716 | codestorywithMIKcodestorywithMIK · 27:50 · 4K views
  3. Calculate Money in Leetcode Bank | 2 Approaches | O(n) | O(1) | Leetcode 1716codestorywithMIK · 27:09 · 3K views
  4. 1716. Calculate Money in Leetcode Bank | Time - O(1) | Math | Arithmetic ProgressionAryan Mittal · 14:39 · 1K views
  5. Calculate Money in LeetCode Bank | LeetCode 1716 | EnglishCodeClips with Abhishek Ranjan · 8:33 · 1K views
  6. 1716. Calculate Money in Leetcode Bank - Daily Leetcode (Day 71)yeetcode · 1:51 · 1K views
  7. 1716. Calculate Money in Leetcode Bank (Leetcode Easy)Programming Live with Larry · 4:16 · 916 views
  8. Calculate Money in Leetcode Bank | LeetCode 1716 | Most Optimal SolutionSanyam IIT Guwahati · 11:42 · 579 views
  9. Daily Leetcode #2034: Oct 25, 2025 - 1716. Calculate Money in Leetcode BankProgramming Live with Larry · 14:03 · 491 views
  10. Calculate Money in Leetcode Bank | Leetcode 1716 | DSA | Code With DeepakCode with Deepak · 13:51 · 443 views

The problem

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.

def totalMoney(n: int) -> int:
    total = 0
    week = 0
    for i in range(n):
        if i % 7 == 0:
            week += 1
        total += week + (i % 7)
    return total

print(totalMoney(10))
class Solution {
    public int totalMoney(int n) {
        int total = 0;
        int week = 0;
        for (int i = 0; i < n; i++) {
            if (i % 7 == 0) {
                week++;
            }
            total += week + (i % 7);
        }
        return total;
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        System.out.println(sol.totalMoney(10));
    }
}
#include <iostream>

int totalMoney(int n) {
    int total = 0;
    int week = 0;
    for (int i = 0; i < n; i++) {
        if (i % 7 == 0) {
            week++;
        }
        total += week + (i % 7);
    }
    return total;
}

int main() {
    int n = 10;
    std::cout << totalMoney(n);
    return 0;
}
function totalMoney(n) {
    let total = 0;
    let week = 0;
    for (let i = 0; i < n; i++) {
        if (i % 7 === 0) {
            week++;
        }
        total += week + (i % 7);
    }
    return total;
}

console.log(totalMoney(10));
#include <stdio.h>

int totalMoney(int n) {
    int total = 0;
    int week = 0;
    for (int i = 0; i < n; i++) {
        if (i % 7 == 0) {
            week++;
        }
        total += week + (i % 7);
    }
    return total;
}

int main() {
    int n = 10;
    printf("%d", totalMoney(n));
    return 0;
}
using System;

public class Solution {
    public int TotalMoney(int n) {
        int total = 0;
        int week = 0;
        for (int i = 0; i < n; i++) {
            if (i % 7 == 0) {
                week++;
            }
            total += week + (i % 7);
        }
        return total;
    }
    public static void Main() {
        Solution sol = new Solution();
        Console.WriteLine(sol.TotalMoney(10));
    }
}

The Python solution also follows an iterative paradigm, looping through days and using modulo to adjust for week changes.

Complexity

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

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.

def totalMoney(n: int) -> int:
    weeks = n // 7
    days = n % 7
    total = (28 + 7 * weeks) * weeks // 2 + (weeks + 1) * days + days * (days - 1) // 2
    return total

print(totalMoney(10))
class Solution {
    public int totalMoney(int n) {
        int weeks = n / 7;
        int days = n % 7;
        int total = (28 + 7 * weeks) * weeks / 2 + (weeks + 1) * days + days * (days - 1) / 2;
        return total;
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        System.out.println(sol.totalMoney(10));
    }
}
#include <iostream>

int totalMoney(int n) {
    int weeks = n / 7;
    int days = n % 7;
    int total = (28 + 7 * weeks) * weeks / 2 + (weeks + 1) * days + days * (days - 1) / 2;
    return total;
}

int main() {
    int n = 10;
    std::cout << totalMoney(n);
    return 0;
}
function totalMoney(n) {
    let weeks = Math.floor(n / 7);
    let days = n % 7;
    let total = (28 + 7 * weeks) * weeks / 2 + (weeks + 1) * days + days * (days - 1) / 2;
    return total;
}

console.log(totalMoney(10));
#include <stdio.h>

int totalMoney(int n) {
    int weeks = n / 7;
    int days = n % 7;
    int total = (28 + 7 * weeks) * weeks / 2 + (weeks + 1) * days + days * (days - 1) / 2;
    return total;
}

int main() {
    int n = 10;
    printf("%d", totalMoney(n));
    return 0;
}
using System;

public class Solution {
    public int TotalMoney(int n) {
        int weeks = n / 7;
        int days = n % 7;
        int total = (28 + 7 * weeks) * weeks / 2 + (weeks + 1) * days + days * (days - 1) / 2;
        return total;
    }
    public static void Main() {
        Solution sol = new Solution();
        Console.WriteLine(sol.TotalMoney(10));
    }
}

This Python code leverages integer division and sum formulas to provide optimal results without loops, maximizing efficiency.

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)$.

function totalMoney(n: number): number {
    const k = (n / 7) | 0;
    const b = n % 7;
    const s1 = (((28 + 28 + 7 * (k - 1)) * k) / 2) | 0;
    const s2 = (((k + 1 + k + 1 + b - 1) * b) / 2) | 0;
    return s1 + s2;
}
func totalMoney(n int) int {
	k, b := n/7, n%7
	s1 := (28 + 28 + 7*(k-1)) * k / 2
	s2 := (k + 1 + k + 1 + b - 1) * b / 2
	return s1 + s2
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Iterative Approach

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

Mathematical Approach

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

Math–

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

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.

Comments

Now write it yourselfClose this page and solve Calculate Money in Leetcode Bank in the editor, without looking.
Practise Calculate Money in Leetcode Bank