Created
October 25, 2025 17:39
-
-
Save tatsuyax25/ed837ccd67626f28fee6f7a6d437d518 to your computer and use it in GitHub Desktop.
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,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| // Function to claculate the total amount of money saved over 'n' days | |
| var totalMoney = function(n) { | |
| let total = 0; // Initialize total money saved | |
| let weekDay = 1; // Start from the first day of the week | |
| let weekNumber = 1; // Start from the first week | |
| // Loop over each day | |
| for (let day = 1; day <= n; day++) { | |
| total += weekDay + weekNumber - 1; // Add the day's money to the total | |
| weekDay++; // Move to the next day | |
| // Check if the week is complete | |
| if (weekDay > 7) { | |
| weekDay = 1; // Reset to the first day of the next week | |
| weekNumber++; // Move to the next week | |
| } | |
| } | |
| return total; // Return the total money saved | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment