Calculate Money in Leetcode Bank - Problem
Hercy's Car Fund Challenge ๐Ÿš—๐Ÿ’ฐ

Hercy is saving up for his dream car and has developed a unique saving strategy using the Leetcode bank. His plan follows a progressive weekly pattern with an interesting twist!

The Pattern:
โ€ข Week 1: Monday=$1, Tuesday=$2, Wednesday=$3, Thursday=$4, Friday=$5, Saturday=$6, Sunday=$7
โ€ข Week 2: Monday=$2, Tuesday=$3, Wednesday=$4, Thursday=$5, Friday=$6, Saturday=$7, Sunday=$8
โ€ข Week 3: Monday=$3, Tuesday=$4, Wednesday=$5, Thursday=$6, Friday=$7, Saturday=$8, Sunday=$9
โ€ข And so on...

Each week, he starts with $1 more than the previous Monday, then adds $1 each subsequent day of that week.

Goal: Given n days, calculate the total amount Hercy will have saved by the end of day n.

Input & Output

example_1.py โ€” Basic Week
$ Input: n = 4
โ€บ Output: 10
๐Ÿ’ก Note: Day 1 (Monday): $1, Day 2 (Tuesday): $2, Day 3 (Wednesday): $3, Day 4 (Thursday): $4. Total = 1+2+3+4 = $10
example_2.py โ€” 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.py โ€” Multiple Weeks
$ Input: n = 20
โ€บ Output: 96
๐Ÿ’ก Note: Week 1: $28, Week 2: $35, Week 3 (6 days): $3+$4+$5+$6+$7+$8 = $33. Total = $28 + $35 + $33 = $96

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • n represents the number of days
  • Each day's deposit is always positive

Visualization

Tap to expand
Hercy's Weekly Saving PatternWeek 1 Pattern:Mon($1) โ†’ Tue($2) โ†’ Wed($3) โ†’ Thu($4) โ†’ Fri($5) โ†’ Sat($6) โ†’ Sun($7) = $28Week 2 Pattern:Mon($2) โ†’ Tue($3) โ†’ Wed($4) โ†’ Thu($5) โ†’ Fri($6) โ†’ Sat($7) โ†’ Sun($8) = $35Week 3 Pattern:Mon($3) โ†’ Tue($4) โ†’ Wed($5) โ†’ Thu($6) โ†’ Fri($7) โ†’ Sat($8) โ†’ Sun($9) = $42Mathematical Formula DiscoveryWeek k total = k + (k+1) + (k+2) + (k+3) + (k+4) + (k+5) + (k+6)= 7k + (0+1+2+3+4+5+6) = 7k + 21๐ŸŽฏ Key Insight: Each week contributes 7k + 21 dollarsSum of first n complete weeks = ฮฃ(7k + 21) = 7ร—ฮฃk + 21n
Understanding the Visualization
1
Identify the Pattern
Each week starts $1 higher than previous week's Monday
2
Calculate Complete Weeks
Use arithmetic series: Week k contributes 7k + 21 dollars
3
Handle Partial Week
Calculate remaining days starting from next Monday's amount
Key Takeaway
๐ŸŽฏ Key Insight: Recognition that weekly totals form an arithmetic sequence allows us to replace O(n) simulation with O(1) mathematical calculation using arithmetic series formulas.
Asked in
Meta 15 Amazon 12 Google 8 Microsoft 6
42.0K Views
Medium Frequency
~15 min Avg. Time
1.5K 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