1716. Calculate Money in Leetcode Bank LeetCode Solution

In this guide, you will get 1716. Calculate Money in Leetcode Bank LeetCode Solution with the best time and space complexity. The solution to Calculate Money in Leetcode Bank problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Calculate Money in Leetcode Bank solution in C++
  4. Calculate Money in Leetcode Bank solution in Java
  5. Calculate Money in Leetcode Bank solution in Python
  6. Additional Resources
1716. Calculate Money in Leetcode Bank LeetCode Solution image

Problem Statement of 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 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 the nth day.

Example 1:

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

Example 2:

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:

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.

See also  187. Repeated DNA Sequences LeetCode Solution

Constraints:

1 <= n <= 1000

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

1716. Calculate Money in Leetcode Bank LeetCode Solution in C++

class Solution {
 public:
  int totalMoney(int n) {
    const int weeks = n / 7;
    const int firstWeek = trapezoid(1, 7);
    const int lastFullWeek = trapezoid(1 + weeks - 1, 7 + weeks - 1);
    const int remainingDays = trapezoid(1 + weeks, n % 7 + weeks);
    return (firstWeek + lastFullWeek) * weeks / 2 + remainingDays;
  }

  // Returns sum(a..b).
  int trapezoid(int a, int b) {
    return (a + b) * (b - a + 1) / 2;
  }
};
/* code provided by PROGIEZ */

1716. Calculate Money in Leetcode Bank LeetCode Solution in Java

class Solution {
  public int totalMoney(int n) {
    final int weeks = n / 7;
    final int firstWeek = trapezoid(1, 7);
    final int lastFullWeek = trapezoid(1 + weeks - 1, 7 + weeks - 1);
    final int remainingDays = trapezoid(1 + weeks, n % 7 + weeks);
    return (firstWeek + lastFullWeek) * weeks / 2 + remainingDays;
  }

  // Returns sum(a..b).
  private int trapezoid(int a, int b) {
    return (a + b) * (b - a + 1) / 2;
  }
}
// code provided by PROGIEZ

1716. Calculate Money in Leetcode Bank LeetCode Solution in Python

class Solution:
  def totalMoney(self, n: int) -> int:
    def trapezoid(a: int, b: int) -> int:
      """Returns sum(a..b)."""
      return (a + b) * (b - a + 1) // 2

    weeks = n // 7
    firstWeek = trapezoid(1, 7)
    lastFullWeek = trapezoid(1 + weeks - 1, 7 + weeks - 1)
    remainingDays = trapezoid(1 + weeks, n % 7 + weeks)
    return (firstWeek + lastFullWeek) * weeks // 2 + remainingDays
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.