LeetCode problem 1716. Calculate Money in Leetcode Bank
requires you to calculate the amount of money in the Leetcode bank after n
days. The rules for the bank deposits are as follows:
$1
on Monday.$2
on Tuesday, $3
on Wednesday, and so on until $7
on Sunday).$(7 + 1)
for the second week, $(7 + 2)
for the third week, etc.).The goal is to return the total amount of money in the bank after n
days.
Q: What are the constraints on n
?
A: 1 <= n <= 1000
Q: Are there any special cases to consider, such as n
being a small number (like 1
or 7
)?
A: Yes, handling small numbers should conform to the general rules applied for the problem.
i
having a deposit of $i
.$1
each subsequent day until Sunday.weeks
are easy to calculate since they repeat periodically.def totalMoney(n: int) -> int:
# Full weeks
full_weeks = n // 7
# Remaining days after full weeks
remaining_days = n % 7
# Calculate money from complete weeks
total_money = 0
for week in range(full_weeks):
# For week i (0-indexed), the deposits start from 1+week till 7+week
total_money += sum(range(1 + week, 8 + week))
# Calculate money from the remaining days
start_day = 1 + full_weeks # the day number in sequence after full_weeks weeks
for day in range(remaining_days):
total_money += start_day + day
return total_money
# Example usage:
print(totalMoney(10)) # Expected output: 37
The time complexity of the solution is O(1)
because the number of weeks (n / 7
) is limited by the constraints, making the loop iterations constant time. Calculating the sums of each week and the remaining days is done with a finite number of operations independent of the input size n
. Hence, the code performs efficiently even at the upper limit of constraints (i.e., n = 1000
).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?