We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Calculate Money in Leetcode Bank

Number: 1817

Difficulty: Easy

Paid? No

Companies: Amazon, Yahoo, Adobe, edabit


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.

  1. Let fullWeeks = n // 7 and extraDays = n % 7.
  2. 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).
  3. The value of Monday’s deposit increases by 1 each week, starting from 1.
  4. Sum the deposits of all the full weeks using the arithmetic series sum formula.
  5. 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.
  6. Return the combined sum from both parts.

Code Solutions

# Python implementation with line-by-line comments

def totalMoney(n):
    # Calculate number of full weeks and extra days
    full_weeks = n // 7
    extra_days = n % 7

    # Sum for all complete weeks using arithmetic series for week sums
    total_full_weeks = 0
    # For each complete week i (0-indexed), Monday's deposit is (1 + i)
    for i in range(full_weeks):
        monday_deposit = 1 + i
        # Sum for the week: deposits are monday_deposit, monday_deposit+1, ..., monday_deposit+6
        week_sum = 7 * monday_deposit + 21  # 21 is the sum of 0..6
        total_full_weeks += week_sum

    # Sum for the remaining extra days for the incomplete week
    # The starting deposit for this week is 1 + full_weeks
    extra_sum = 0
    monday_deposit_extra = 1 + full_weeks
    for j in range(extra_days):
        extra_sum += monday_deposit_extra + j

    # Total amount is the sum of full weeks and extra days
    return total_full_weeks + extra_sum

# Example usage:
print(totalMoney(4))   # Output: 10
print(totalMoney(10))  # Output: 37
print(totalMoney(20))  # Output: 96
← Back to All Questions