1716. Calculate Money in Leetcode Bank
Problem Description
Hercy is saving money following a specific pattern. He deposits money into his bank account every day according to these rules:
Week 1:
- Monday: $1
- Tuesday: $2
- Wednesday: $3
- Thursday: $4
- Friday: $5
- Saturday: $6
- Sunday: $7
Week 2:
- Monday: 1)
- Tuesday: $3
- Wednesday: $4
- Thursday: $5
- Friday: $6
- Saturday: $7
- Sunday: $8
Pattern: Each week starts on Monday with 1 each day from Monday to Sunday.
Given an integer n
representing the number of days, calculate the total amount of money Hercy will have saved after n
days.
Example walkthrough:
- If
n = 10
, this covers 1 full week plus 3 days into the second week - Week 1: 2 + 4 + 6 + 28
- Week 2 (first 3 days): 3 + 9
- Total: 9 = $37
The solution uses mathematical formulas to calculate:
- The sum for complete weeks using the arithmetic progression formula
- The sum for any remaining partial week
- The formula
(28 + 28 + 7 * (a - 1)) * a // 2
calculates the total fora
complete weeks - The formula
(a * 2 + b + 1) * b // 2
calculates the total for the remainingb
days
Intuition
Let's first observe the deposit pattern more carefully. If we look at complete weeks:
- Week 1: 1, 2, 3, 4, 5, 6, 7 (total = 28)
- Week 2: 2, 3, 4, 5, 6, 7, 8 (total = 35)
- Week 3: 3, 4, 5, 6, 7, 8, 9 (total = 42)
Notice that each complete week's total increases by 7 from the previous week. This forms an arithmetic sequence: 28, 35, 42, 49, ...
For a
complete weeks, we need the sum of this arithmetic sequence. Using the arithmetic series formula sum = (first_term + last_term) * count / 2
:
- First week total: 28
- Last (a-th) week total:
28 + 7 * (a - 1)
- Sum of
a
weeks:(28 + 28 + 7 * (a - 1)) * a / 2
Now for the remaining b
days (where n = 7 * a + b
), these days start from week (a + 1)
:
- The Monday of week
(a + 1)
would have deposit:a + 1
- The pattern for
b
days would be:(a + 1), (a + 2), (a + 3), ..., (a + b)
This is another arithmetic sequence with:
- First term:
a + 1
- Last term:
a + b
- Sum:
((a + 1) + (a + b)) * b / 2 = (2a + b + 1) * b / 2
By dividing n
by 7 using divmod(n, 7)
, we get:
a
= number of complete weeksb
= remaining days
The total money is simply the sum of money from complete weeks plus money from remaining days.
Learn more about Math patterns.
Solution Approach
The implementation uses a mathematical approach to directly calculate the total money without simulating day-by-day deposits.
Step 1: Divide the days into complete weeks and remaining days
a, b = divmod(n, 7)
a
represents the number of complete weeksb
represents the remaining days (0 to 6)
Step 2: Calculate the sum for complete weeks
The formula (28 + 28 + 7 * (a - 1)) * a // 2
calculates the total for all complete weeks:
- Each complete week forms an arithmetic sequence: 28, 35, 42, ...
- First term: 28
- Last term:
28 + 7 * (a - 1)
- Using the arithmetic series sum formula:
sum = (first + last) * count / 2
- This gives us:
(28 + 28 + 7 * (a - 1)) * a // 2
Step 3: Calculate the sum for remaining days
The formula (a * 2 + b + 1) * b // 2
calculates the total for the partial week:
- The remaining
b
days start from week(a + 1)
- Monday of week
(a + 1)
has deposit:a + 1
- The deposits form the sequence:
(a + 1), (a + 2), ..., (a + b)
- First term:
a + 1
- Last term:
a + b
- Sum:
((a + 1) + (a + b)) * b / 2 = (2a + b + 1) * b / 2
Step 4: Return the total
return (28 + 28 + 7 * (a - 1)) * a // 2 + (a * 2 + b + 1) * b // 2
The final answer is the sum of money from complete weeks plus money from the remaining partial week. The use of integer division //
ensures the result is an integer.
Time Complexity: O(1)
- Only arithmetic operations are performed
Space Complexity: O(1)
- Only a constant amount of extra space is used
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through the solution with n = 17
days.
Step 1: Divide into complete weeks and remaining days
a, b = divmod(17, 7) # a = 2, b = 3
So we have 2 complete weeks and 3 remaining days.
Step 2: Calculate money from complete weeks
Week 1 deposits: 1, 2, 3, 4, 5, 6, 7 → Total = 28 Week 2 deposits: 2, 3, 4, 5, 6, 7, 8 → Total = 35
Using our formula for complete weeks:
(28 + 28 + 7 * (a - 1)) * a // 2
= (28 + 28 + 7 * (2 - 1)) * 2 // 2
= (28 + 28 + 7) * 2 // 2
= 63 * 2 // 2
= 63
We can verify: 28 + 35 = 63 ✓
Step 3: Calculate money from remaining 3 days
The 3 remaining days are in week 3:
- Day 15 (Monday of week 3): $3
- Day 16 (Tuesday of week 3): $4
- Day 17 (Wednesday of week 3): $5
Using our formula for remaining days:
(a * 2 + b + 1) * b // 2
= (2 * 2 + 3 + 1) * 3 // 2
= 8 * 3 // 2
= 24 // 2
= 12
We can verify: 3 + 4 + 5 = 12 ✓
Step 4: Calculate total
Total = 63 + 12 = 75
Therefore, after 17 days, Hercy will have saved $75.
Solution Implementation
1class Solution:
2 def totalMoney(self, n: int) -> int:
3 # Calculate complete weeks and remaining days
4 complete_weeks, remaining_days = divmod(n, 7)
5
6 # Calculate sum for complete weeks
7 # Each week starts with 1 more than the previous week's start
8 # Week 1: 1+2+3+4+5+6+7 = 28
9 # Week 2: 2+3+4+5+6+7+8 = 35 (28 + 7)
10 # Week 3: 3+4+5+6+7+8+9 = 42 (28 + 14)
11 # This forms an arithmetic sequence: 28, 35, 42, ...
12 # Sum = (first_term + last_term) * num_terms / 2
13 first_week_sum = 28
14 last_week_sum = 28 + 7 * (complete_weeks - 1)
15 total_from_complete_weeks = (first_week_sum + last_week_sum) * complete_weeks // 2
16
17 # Calculate sum for remaining days
18 # Remaining days start from (complete_weeks + 1) and increment by 1 each day
19 # Forms an arithmetic sequence starting at (complete_weeks + 1)
20 # Sum = (first_day + last_day) * num_days / 2
21 first_day_amount = complete_weeks + 1
22 last_day_amount = complete_weeks + remaining_days
23 total_from_remaining_days = (first_day_amount + last_day_amount) * remaining_days // 2
24
25 return total_from_complete_weeks + total_from_remaining_days
26
1class Solution {
2 public int totalMoney(int n) {
3 // Calculate the number of complete weeks and remaining days
4 int completeWeeks = n / 7;
5 int remainingDays = n % 7;
6
7 // Calculate the sum for all complete weeks
8 // First week sum: 28 (1+2+3+4+5+6+7)
9 // Each subsequent week adds 7 more than the previous week
10 // This forms an arithmetic sequence: 28, 35, 42, ...
11 // Sum of arithmetic sequence: (first term + last term) * number of terms / 2
12 int firstWeekSum = 28;
13 int lastWeekSum = 28 + 7 * (completeWeeks - 1);
14 int totalFromCompleteWeeks = (firstWeekSum + lastWeekSum) * completeWeeks / 2;
15
16 // Calculate the sum for the remaining days
17 // Starting amount for remaining days: completeWeeks + 1
18 // The amounts form an arithmetic sequence starting from (completeWeeks + 1)
19 // Sum = (first day + last day) * number of days / 2
20 int firstDayOfRemainingWeek = completeWeeks + 1;
21 int lastDayOfRemainingWeek = completeWeeks + remainingDays;
22 int totalFromRemainingDays = (firstDayOfRemainingWeek + lastDayOfRemainingWeek) * remainingDays / 2;
23
24 return totalFromCompleteWeeks + totalFromRemainingDays;
25 }
26}
27
1class Solution {
2public:
3 int totalMoney(int n) {
4 // Calculate number of complete weeks and remaining days
5 int completeWeeks = n / 7;
6 int remainingDays = n % 7;
7
8 // Calculate total money from complete weeks
9 // Each week starts with 1 more dollar than the previous week
10 // Week 1: 1+2+3+4+5+6+7 = 28
11 // Week 2: 2+3+4+5+6+7+8 = 35 (28 + 7)
12 // Week 3: 3+4+5+6+7+8+9 = 42 (28 + 14)
13 // This forms an arithmetic sequence: 28, 35, 42, ...
14 // Sum = (first term + last term) * number of terms / 2
15 int firstWeekSum = 28;
16 int lastWeekSum = 28 + 7 * (completeWeeks - 1);
17 int totalFromCompleteWeeks = (firstWeekSum + lastWeekSum) * completeWeeks / 2;
18
19 // Calculate total money from remaining days
20 // Remaining days start from (completeWeeks + 1) on Monday
21 // Forms an arithmetic sequence starting from (completeWeeks + 1)
22 // Sum = (first term + last term) * number of terms / 2
23 int firstDayAmount = completeWeeks + 1;
24 int lastDayAmount = completeWeeks + remainingDays;
25 int totalFromRemainingDays = (firstDayAmount + lastDayAmount) * remainingDays / 2;
26
27 // Return the total sum
28 return totalFromCompleteWeeks + totalFromRemainingDays;
29 }
30};
31
1function totalMoney(n: number): number {
2 // Calculate number of complete weeks and remaining days
3 const completeWeeks: number = Math.floor(n / 7);
4 const remainingDays: number = n % 7;
5
6 // Calculate total money from complete weeks
7 // Each week starts with 1 more dollar than the previous week
8 // Week 1: 1+2+3+4+5+6+7 = 28
9 // Week 2: 2+3+4+5+6+7+8 = 35 (28 + 7)
10 // Week 3: 3+4+5+6+7+8+9 = 42 (28 + 14)
11 // This forms an arithmetic sequence: 28, 35, 42, ...
12 // Sum = (first term + last term) * number of terms / 2
13 const firstWeekSum: number = 28;
14 const lastWeekSum: number = 28 + 7 * (completeWeeks - 1);
15 const totalFromCompleteWeeks: number = (firstWeekSum + lastWeekSum) * completeWeeks / 2;
16
17 // Calculate total money from remaining days
18 // Remaining days start from (completeWeeks + 1) on Monday
19 // Forms an arithmetic sequence starting from (completeWeeks + 1)
20 // Sum = (first term + last term) * number of terms / 2
21 const firstDayAmount: number = completeWeeks + 1;
22 const lastDayAmount: number = completeWeeks + remainingDays;
23 const totalFromRemainingDays: number = (firstDayAmount + lastDayAmount) * remainingDays / 2;
24
25 // Return the total sum
26 return totalFromCompleteWeeks + totalFromRemainingDays;
27}
28
Time and Space Complexity
Time Complexity: O(1)
The code performs a fixed number of arithmetic operations regardless of the input size n
:
- One division with remainder operation (
divmod
) - A constant number of arithmetic operations (additions, subtractions, multiplications, divisions)
- No loops or recursive calls
All operations execute in constant time, making the overall time complexity O(1)
.
Space Complexity: O(1)
The code uses only a fixed amount of extra space:
- Two variables
a
andb
to store the quotient and remainder fromdivmod(n, 7)
- No additional data structures that grow with input size
- No recursive calls that would use stack space
The space usage remains constant regardless of the value of n
, resulting in O(1)
space complexity.
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Off-by-One Error in Week Calculation
A common mistake is incorrectly calculating the starting amount for each week. Students often think week k
starts with amount k
instead of k+1
for the Monday deposit.
Incorrect assumption:
- Week 1 Monday: $0
- Week 2 Monday: $1
- Week 3 Monday: $2
Correct pattern:
- Week 1 Monday: $1
- Week 2 Monday: $2
- Week 3 Monday: $3
Solution: Remember that week numbering starts at 1, and Monday of week w
has deposit amount w
.
2. Mishandling Edge Cases
When n = 7
(exactly one week) or when there are no remaining days (b = 0
), the formula for remaining days should correctly evaluate to 0.
Potential issue: Division by zero or incorrect calculation when remaining_days = 0
.
Solution: The formula (a * 2 + b + 1) * b // 2
naturally handles this case since when b = 0
, the entire expression becomes 0 regardless of the value of a
.
3. Integer Overflow in Other Languages
While Python handles arbitrary precision integers, in languages like Java or C++, the multiplication in the formula could cause overflow for large values of n
.
Example: (28 + 28 + 7 * (a - 1)) * a
could overflow before division.
Solution:
- Use appropriate data types (e.g.,
long long
in C++) - Rearrange the formula to perform division earlier if possible
- Consider modular arithmetic if the problem requires it
4. Incorrect Arithmetic Sequence Formula Application
Students might incorrectly apply the arithmetic sequence sum formula by:
- Forgetting to account for the changing starting point each week
- Using wrong first/last terms in the sequence
Common mistake:
# Incorrect: Assuming each week always sums to 28 total_from_complete_weeks = 28 * complete_weeks
Solution: Recognize that each complete week forms a different arithmetic sequence with sum increasing by 7 each week: 28, 35, 42, ...
5. Confusion Between Week Number and Array Indexing
When thinking about "week a+1
", it's easy to confuse:
- Human-readable week numbers (1, 2, 3, ...)
- Zero-based indexing often used in programming
Solution: Stay consistent with the problem's convention where the first week is "Week 1" and Monday of Week 1 has deposit $1.
Given a sorted array of integers and an integer called target, find the element that
equals to the target and return its index. Select the correct code that fills the
___
in the given code snippet.
1def binary_search(arr, target):
2 left, right = 0, len(arr) - 1
3 while left ___ right:
4 mid = (left + right) // 2
5 if arr[mid] == target:
6 return mid
7 if arr[mid] < target:
8 ___ = mid + 1
9 else:
10 ___ = mid - 1
11 return -1
12
1public static int binarySearch(int[] arr, int target) {
2 int left = 0;
3 int right = arr.length - 1;
4
5 while (left ___ right) {
6 int mid = left + (right - left) / 2;
7 if (arr[mid] == target) return mid;
8 if (arr[mid] < target) {
9 ___ = mid + 1;
10 } else {
11 ___ = mid - 1;
12 }
13 }
14 return -1;
15}
16
1function binarySearch(arr, target) {
2 let left = 0;
3 let right = arr.length - 1;
4
5 while (left ___ right) {
6 let mid = left + Math.trunc((right - left) / 2);
7 if (arr[mid] == target) return mid;
8 if (arr[mid] < target) {
9 ___ = mid + 1;
10 } else {
11 ___ = mid - 1;
12 }
13 }
14 return -1;
15}
16
Recommended Readings
Math for Technical Interviews How much math do I need to know for technical interviews The short answer is about high school level math Computer science is often associated with math and some universities even place their computer science department under the math faculty However the reality is that you
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask
Want a Structured Path to Master System Design Too? Don’t Miss This!