class Solution:
def totalMoney(self, n: int) -> int:
weeks = n // 7
days = n % 7
# Money from complete weeks: sum of arithmetic progression
# Each week starts with 1 more than previous week
total = 0
# For complete weeks
total += 7 * weeks * (weeks + 1) // 2
# For leftover days in the last (possibly incomplete) week
total += days * (weeks + 1) + days * (days - 1) // 2
return total
class Solution {
public:
int totalMoney(int n) {
int weeks = n / 7;
int days = n % 7;
int total = 0;
// Complete weeks
total += 7 * weeks * (weeks + 1) / 2;
// Remaining days
total += days * (weeks + 1) + days * (days - 1) / 2;
return total;
}
};
class Solution {
public int totalMoney(int n) {
int weeks = n / 7;
int days = n % 7;
int total = 0;
// Complete weeks
total += 7 * weeks * (weeks + 1) / 2;
// Remaining days
total += days * (weeks + 1) + days * (days - 1) / 2;
return total;
}
}
var totalMoney = function(n) {
const weeks = Math.floor(n / 7);
const days = n % 7;
let total = 0;
// Complete weeks
total += 7 * weeks * (weeks + 1) / 2;
// Remaining days
total += days * (weeks + 1) + days * (days - 1) / 2;
return total;
};
You are given an integer n
representing the number of days. Each day, you deposit money into the Leetcode bank according to the following rules:
$1
. Each subsequent day of the week, you deposit $1
more than the previous day.$1
compared to the previous week.$1
, Tuesday $2
, ..., Sunday $7
. The next Monday you deposit $2
, Tuesday $3
, etc.
Your task is to return the total amount of money in the bank after n
days.
Constraints:
1 <= n <= 1000
The problem requires calculating the total deposits over n
days, where the deposit pattern increases both weekly and daily. At first glance, it might seem natural to simulate each day and sum the deposits. However, this would be inefficient for larger values of n
.
Instead, we can look for patterns. Noticing that each week starts with a higher base deposit and daily increases, we realize that each week forms an arithmetic progression. By breaking the problem into complete weeks and leftover days, we can use formulas to compute the sums efficiently.
The shift from brute-force simulation to mathematical calculation is key for optimization.
weeks = n // 7
(integer division) be the number of complete weeks.days = n % 7
be the number of leftover days in the last (possibly incomplete) week.i
(starting from 0) is the sum of an arithmetic progression: sum = 7 * (i + 1) + 21
, but more generally, the sum for week i
is 7 * (i + 1) + 21
.
n
natural numbers, and sum over all weeks: 7 * weeks * (weeks + 1) / 2
.
weeks + 1
), and increase by 1 each day.
days * (weeks + 1) + days * (days - 1) / 2
.
Let's consider n = 10
:
weeks = 10 // 7 = 1
, days = 10 % 7 = 3
So, after 10 days, the total money in the bank is 37.
The "Calculate Money in Leetcode Bank" problem can be elegantly solved by recognizing the arithmetic progression in the deposit pattern. Instead of simulating each day, we break the problem into complete weeks and leftover days, and use formulas to calculate the sums directly. This reduces the time complexity from O(n) to O(1), making the solution both efficient and clean.
The key insight is to leverage mathematical patterns and formulas for summing sequences, which is a powerful technique for many similar problems.