Calculate Money in Leetcode Bank
Problem
Hercy saves money every day. On the first day (Monday) he deposits $1, and each following day he deposits $1 more than the day before. On every new Monday he resets, depositing $1 more than the previous Monday. Given n, return the total amount in the bank after the nth day.
n = 1037def totalMoney(n):
total = 0
monday = 1 # deposit made on the current week's Monday
day = 1 # day within the week, 1..7
deposit = 1 # amount put in today
for _ in range(n):
total += deposit # save today's deposit
if day == 7: # week finished -> start a new Monday
day = 1
monday += 1 # next Monday is $1 higher
deposit = monday
else: # mid-week -> just add $1
day += 1
deposit += 1
return total
function totalMoney(n) {
let total = 0;
let monday = 1; // deposit made on the current week's Monday
let day = 1; // day within the week, 1..7
let deposit = 1; // amount put in today
for (let i = 0; i < n; i++) {
total += deposit; // save today's deposit
if (day === 7) { // week finished -> start a new Monday
day = 1;
monday += 1; // next Monday is $1 higher
deposit = monday;
} else { // mid-week -> just add $1
day += 1;
deposit += 1;
}
}
return total;
}
int totalMoney(int n) {
int total = 0;
int monday = 1; // deposit made on the current week's Monday
int day = 1; // day within the week, 1..7
int deposit = 1; // amount put in today
for (int i = 0; i < n; i++) {
total += deposit; // save today's deposit
if (day == 7) { // week finished -> start a new Monday
day = 1;
monday += 1; // next Monday is $1 higher
deposit = monday;
} else { // mid-week -> just add $1
day += 1;
deposit += 1;
}
}
return total;
}
int totalMoney(int n) {
int total = 0;
int monday = 1; // deposit made on the current week's Monday
int day = 1; // day within the week, 1..7
int deposit = 1; // amount put in today
for (int i = 0; i < n; i++) {
total += deposit; // save today's deposit
if (day == 7) { // week finished -> start a new Monday
day = 1;
monday += 1; // next Monday is $1 higher
deposit = monday;
} else { // mid-week -> just add $1
day += 1;
deposit += 1;
}
}
return total;
}
Explanation
This is a direct simulation. We walk through the n days one at a time and add up every deposit, so we never have to reason about a closed-form formula.
Three counters describe the savings rhythm: monday is what Hercy deposits on the current week's Monday, day tracks which day of the week we are on (1 through 7), and deposit is today's amount.
Each day we do total += deposit. Then we decide tomorrow's deposit. If day == 7 the week is over: we wrap day back to 1, bump monday by $1, and set tomorrow's deposit to that new Monday value. Otherwise we are still mid-week, so we advance day and simply add $1 to deposit.
Example: n = 10. The first seven days give 1+2+3+4+5+6+7 = 28. The eighth day is a fresh Monday starting at $2, so days 8–10 give 2+3+4 = 9. The total is 28 + 9 = 37.
Because each day costs O(1) work, the whole simulation is linear in n and uses only a handful of integer variables. A constant-time arithmetic-series formula also exists, but the simulation is the clearest faithful model.