Calculate Money in Leetcode Bank - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: n = 4
Output: 10
💡 Note: Day 1 (Mon): $1, Day 2 (Tue): $2, Day 3 (Wed): $3, Day 4 (Thu): $4. Total: 1+2+3+4 = 10
Example 2 — Complete Week
$ Input: n = 10
Output: 37
💡 Note: Week 1: $1+$2+$3+$4+$5+$6+$7 = $28. Week 2 (3 days): $2+$3+$4 = $9. Total: 28+9 = 37
Example 3 — Single Day
$ Input: n = 1
Output: 1
💡 Note: Only Monday of first week: $1

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Calculate Money in Leetcode Bank INPUT Week 1 Deposits Mon Tue Wed Thu Fri Sat $1 $2 $3 $4 $5 $6 n = 4 (First 4 days) Pattern: Each day +$1 Mon: base, Tue: +1, Wed: +2... Next Mon: previous Mon + $1 n = 4 Number of days BANK ALGORITHM STEPS 1 Calculate weeks weeks = n / 7 = 0 2 Remaining days days = n % 7 = 4 3 Sum full weeks Week sum = 1+2+...+7 = 28 4 Add extra days 1 + 2 + 3 + 4 = 10 Calculation Day 1 (Mon): $1 Day 2 (Tue): $2 Day 3 (Wed): $3 Day 4 (Thu): $4 Total: 1+2+3+4 = $10 FINAL RESULT $10 $1 $2 $3 $4 Output: 10 Total savings after 4 days OK Key Insight: Use arithmetic series formula: For complete weeks, sum = 28*weeks + 7*(weeks-1)*weeks/2. For remaining days: sum = days*(days+1)/2 + weeks*days. This gives O(1) time complexity! TutorialsPoint - Calculate Money in Leetcode Bank | Optimal Solution
Asked in
Google 15 Amazon 12
28.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen