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 thenth day.

Examples

Example 1

1
2
3
Input: n = 4
Output: 10
Explanation:  After the 4th day, the total is 1 + 2 + 3 + 4 = 10.

Example 2

1
2
3
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

1
2
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

Solution

Method 1 – Arithmetic Progression (Weekly and Daily Sum)

Intuition

The money deposited each week forms an arithmetic progression, and each day within a week also follows a pattern. We can compute the total by summing complete weeks and then adding the remaining days.

Approach

  1. Calculate the number of complete weeks: weeks = n // 7.
  2. Calculate the number of remaining days: days = n % 7.
  3. For complete weeks:
    • The sum for week i (0-indexed) is 7 * (i + 1) + 21 * i / 2.
    • Or, use the formula for the sum of an arithmetic sequence.
    • Total for all complete weeks: weeks * (7 * weeks + 7) // 2.
  4. For remaining days:
    • The deposit for each day is weeks + day + 1.
    • Sum: days * (2 * weeks + days + 1) // 2.
  5. Add both results and return.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    int totalMoney(int n) {
        int weeks = n / 7, days = n % 7;
        int ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2;
        for (int i = 1; i <= days; ++i) {
            ans += weeks + i;
        }
        return ans;
    }
};
1
2
3
4
5
6
7
8
func totalMoney(n int) int {
    weeks, days := n/7, n%7
    ans := weeks*28 + 7*weeks*(weeks-1)/2
    for i := 1; i <= days; i++ {
        ans += weeks + i
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int totalMoney(int n) {
        int weeks = n / 7, days = n % 7;
        int ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2;
        for (int i = 1; i <= days; ++i) {
            ans += weeks + i;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun totalMoney(n: Int): Int {
        val weeks = n / 7
        val days = n % 7
        var ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2
        for (i in 1..days) {
            ans += weeks + i
        }
        return ans
    }
}
1
2
3
4
5
6
7
class Solution:
    def totalMoney(self, n: int) -> int:
        weeks, days = divmod(n, 7)
        ans = weeks * 28 + 7 * weeks * (weeks - 1) // 2
        for i in range(1, days + 1):
            ans += weeks + i
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
impl Solution {
    pub fn total_money(n: i32) -> i32 {
        let weeks = n / 7;
        let days = n % 7;
        let mut ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2;
        for i in 1..=days {
            ans += weeks + i;
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    totalMoney(n: number): number {
        const weeks = Math.floor(n / 7);
        const days = n % 7;
        let ans = weeks * 28 + 7 * weeks * (weeks - 1) / 2;
        for (let i = 1; i <= days; ++i) {
            ans += weeks + i;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(1)
  • 🧺 Space complexity: O(1)