Problem Description
Hercy deposits money into the Leetcode bank every day. On the first Monday he deposits $1, and every subsequent day (Tuesday to Sunday) he deposits $1 more than the day before. Each new Monday, his deposit amount increases by $1 compared to the previous Monday’s deposit. Given n days, calculate the total amount deposited at the end of the nth day.
Key Insights
- The deposits follow a weekly pattern where within each full week the deposit increases by 1 each day.
- For every full week, the starting deposit (Monday’s deposit) increases by 1.
- The sum for a full week can be calculated using the arithmetic series formula.
- For remaining days (less than a full week), compute the deposit based on the corresponding day's offset from Monday.
- Use integer division and modulo operations to separate full weeks from extra days.
Space and Time Complexity
Time Complexity: O(1) as computation involves constant time arithmetic operations.
Space Complexity: O(1) as only a fixed number of variables are used.
Solution
The solution involves calculating the total sum in two parts: the sum for all complete weeks and the sum for the remaining days.
- Let fullWeeks = n // 7 and extraDays = n % 7.
- For each full week starting with a deposit a on Monday, the sum of that week is: 7 * a + 21 (since the daily increments over 7 days add up to 21).
- The value of Monday’s deposit increases by 1 each week, starting from 1.
- Sum the deposits of all the full weeks using the arithmetic series sum formula.
- For the remaining days, the deposit for day j (0-indexed) in the incomplete week is given by starting deposit (which is 1 + fullWeeks) plus j, and sum these values.
- Return the combined sum from both parts.