1716. Calculate Money in Leetcode Bank
Difficulty: Easy
Topics: Math, Biweekly Contest 43
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 nᵗʰ day.
Example 1:
- Input: n = 4
- Output: 10
- Explanation: After the 4ᵗʰ day, the total is 1 + 2 + 3 + 4 = 10.
Example 2:
- Input: n = 10
- Output: 37
- Explanation: After the 10ᵗʰ day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2ⁿᵈ Monday, Hercy only puts in $2.
Example 3:
- Input: n = 20
- Output: 96
- Explanation: After the 20ᵗʰ 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
Hint:
- Simulate the process by keeping track of how much money Hercy is putting in and which day of the week it is, and use this information to deduce how much money Hercy will put in the next day.
Solution:
We need to calculate the total amount of money Hercy saves in the Leetcode bank over n days. Hercy starts by putting in $1 on the first day (Monday) and increases the amount by $1 each subsequent day. On every following Monday, he increases the base amount by $1 compared to the previous Monday.
Approach
- Problem Analysis: The problem involves calculating the sum of an increasing sequence where each week the starting amount increases by $1. The sequence for each week starts from the week number (1 for the first week, 2 for the second, etc.) and increases by $1 each day for up to 7 days in a week.
-
Key Insight: The total days
ncan be divided into complete weeks and remaining days. For each complete week, the sum can be calculated using the arithmetic series formula. The remaining days are handled by calculating the sum starting from the next week's base amount. - Algorithm Selection: Use integer division to determine the number of complete weeks and the remaining days. For each complete week, compute the sum of the arithmetic series starting from the week number up to the week number plus 6. For the remaining days, compute the sum starting from the next week's base amount up to the base amount plus the number of remaining days minus one.
Let's implement this solution in PHP: 1716. Calculate Money in Leetcode Bank
<?php
/**
* @param Integer $n
* @return Integer
*/
function totalMoney($n) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo totalMoney(4) . "\n"; // Output: 10
echo totalMoney(10) . "\n"; // Output: 37
echo totalMoney(20) . "\n"; // Output: 96
?>
Explanation:
-
Initialization: Initialize
totalto accumulate the total savings. -
Complete Weeks Calculation: Calculate the number of complete weeks (
weeks) and remaining days (remainingDays). For each complete week, compute the sum of the arithmetic series from the starting amount (week number) to the starting amount plus 6 (the Sunday amount). - Remaining Days Calculation: For the remaining days, compute the sum from the next week's base amount up to the base amount plus the number of remaining days minus one.
- Return Result: The accumulated total is returned after processing all complete weeks and remaining days.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
![]()
If you want more helpful content like this, feel free to follow me:
Top comments (0)