algoadvance

Leetcode 1716. Calculate Money in Leetcode Bank

Problem Statement

  1. Calculate Money in Leetcode Bank

Hercy wants to save money for his first car. He puts money in the Leetcode bank every day.

He starts by putting in 1 dollar on Monday, the first day. Every subsequent day, he will put in more money than the previous day by 1 dollar.

Every Monday, he will restart by putting 1 dollar in the Leetcode bank.

Given n, the total number of days Hercy will save money, return the total amount of money he will have in the Leetcode bank at the end of n days.

Example 1:

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

Example 2:

Input: n = 10
Output: 37
Explanation: After 10 days, the total money is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (1 + 2 + 3) = 28 + 9 = 37.

Example 3:

Input: n = 20
Output: 96
Explanation: After 20 days, the total money is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (1 + 2 + 3 + 4 + 5 + 6 + 7) + (1 + 2 + 3 + 4 + 5 + 6) = 28 + 28 + 21 = 77.

Clarifying Questions

  1. What is the range of n?
    • The problem constraints mention 1 <= n <= 1000.
  2. Can n be zero or a negative number?
    • No, as per the problem constraints, n is always a positive integer.
  3. Do we need to account for different days of the week?
    • Yes, each week begins with 1 dollar on Monday and increments each subsequent day.

Strategy

  1. Initialize a variable to keep track of the total money accumulated.
  2. Iterate over each day from 1 to n:
    • Calculate the number of complete weeks that have passed using week = (day - 1) / 7.
    • Calculate the daily increment using day_in_week = (day - 1) % 7.
    • The amount to deposit each day will be week + day_in_week + 1.
  3. Sum up all daily deposits for n days.
  4. Return the total money.

Time Complexity

The algorithm will have a time complexity of O(n) since it iterates through each of the n days once.

Code

#include <iostream>
#include <cmath>

class Solution {
public:
    int totalMoney(int n) {
        int total_money = 0;
        for (int day = 1; day <= n; ++day) {
            int week = (day - 1) / 7;
            int day_in_week = (day - 1) % 7;
            total_money += (week + day_in_week + 1);
        }
        return total_money;
    }
};

int main() {
    Solution solution;
    int n;

    // Example 1
    n = 4;
    std::cout << "Result for n = " << n << ": " << solution.totalMoney(n) << std::endl;  // Output: 10

    // Example 2
    n = 10;
    std::cout << "Result for n = " << n << ": " << solution.totalMoney(n) << std::endl;  // Output: 37

    // Example 3
    n = 20;
    std::cout << "Result for n = " << n << ": " << solution.totalMoney(n) << std::endl;  // Output: 96

    return 0;
}

This code initializes a total money counter, iterates through each day, calculates the savings increment correctly considering both week and day within the week, and sums up the total savings by the end of n days.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI