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:
โข
โข
โข
โข 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
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code