Similar Problems

Similar Problems not available

Calculate Money In Leetcode Bank - Leetcode Solution

Companies:

LeetCode:  Calculate Money In Leetcode Bank Leetcode Solution

Difficulty: Easy

Topics: math  

Problem Statement:

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 puts in $1 more than the day before. On every subsequent Monday, he puts 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) + (8 + 9) + 10 = 37.

Solution:

In this problem, we have to find the total amount of money Herfy will have at the end of the nth day.

To solve the problem, we can use a simple loop to calculate the total amount of money.

We can initialize the total amount to 0 and then loop through the days from 1 to n. For each day, we can calculate the money that Herfy puts in the bank and add it to the total amount.

As per the problem statement, Herfy puts in $1 on Monday, the first day, and then $1 more than the day before every day from Tuesday to Sunday.

On every subsequent Monday, he puts in $1 more than the previous Monday.

To implement this behavior, we can use the % operator to check if it is a Monday or not.

If it is a Monday, then we can update the money added to the bank to 1, and then update the value of the next Monday's increase by adding 1 to it.

If it is not a Monday, then we can simply add a value of 1 to the previous day's value.

Here is the Python code for the solution:

def totalMoney(n: int) -> int:
    total = 0
    monday_increase = 1
    for day in range(1, n+1):
        if day % 7 == 1:
            total += monday_increase
            monday_increase += 1
        else:
            total += (day % 7) + (monday_increase - 1)
    return total

The above code with take an integer value n as input, and it will return the total amount of money in Herfy's bank account at the end of the nth day.

For example, if we call the function with n=4, it will return 10, and if we call it with n=10, it will return 37, as expected.

In conclusion, we can solve this problem by using a simple loop, a few if-else statements, and the modulus operator to calculate the money that Herfy puts in the Leetcode bank every day.

Calculate Money In Leetcode Bank Solution Code

1