Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created October 25, 2025 05:23
Show Gist options
  • Select an option

  • Save Ifihan/79e2564bed7c1c9bdb73bf0333d9ce26 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/79e2564bed7c1c9bdb73bf0333d9ce26 to your computer and use it in GitHub Desktop.
Calculate Money in Leetcode Bank

Question

Approach

I observe that Hercy saves money in weekly cycles. Each week starts on Monday with an increasing base amount. The first week starts with 1, the second week starts with 2, and so on. For each full week, the total is the sum of 7 consecutive numbers starting from that base. So, for w full weeks, I can compute the sum using the arithmetic series formula. For the remaining r days (less than a week), I add the first r terms of the next week's sequence.

Implementation

class Solution:
    def totalMoney(self, n: int) -> int:
        weeks, days = divmod(n, 7)
        total = 7 * weeks * (weeks + 1) // 2 + 21 * weeks 
        total += days * (2 * weeks + days + 1) // 2
        return total

Complexities

  • Time: O(1)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment