Calculate Money in Leetcode Bank

easy math simulation arithmetic series

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.

Inputn = 10
Output37
Week 1: 1+2+3+4+5+6+7 = 28. Then the next Monday starts at $2: 2+3+4 = 9. Total = 28 + 9 = 37.

def 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;
}
Time: O(n) Space: O(1)