leetcode-1716. Calculate Money in Leetcode Bank

Youngsun Joung·2025년 10월 28일

Leetcode

목록 보기
13/65

1. 문제 소개

Constraints:

  • 1 <= n <= 1000

1716. Calculate Money in Leetcode Bank

2. 나의 풀이법

divmod()를 사용해 몫q과 나머지r를 구하고, 이에 맞춰서 셈을 했다.
시간 복잡도는 O(q)O(q)이다.

class Solution:
    def totalMoney(self, n: int) -> int:
        q, r = divmod(n, 7)
        print(q, r)

        if q > 0:
            ans = 0
            multi = 0
            while q > 0:
                ans += 28 + 7 * multi
                q -= 1
                multi += 1
            ans += sum(range(1, r+1)) + r * multi
            return ans
        else:
            return sum(range(1, r+1))

3. 다른 풀이법

물론 아래처럼 공식을 전부 사용해 시간복잡도가 O(1)O(1)이 되도록 풀 수 있다.

class Solution:
    def totalMoney(self, n: int) -> int:
        q, r = divmod(n, 7)
        full_weeks = 7 * q * (q + 1) // 2 + 21 * q
        rem_days = r * (q + 1) + r * (r - 1) // 2
        return full_weeks + rem_days

4. 결론

수학적 공식을 사용할 수 있다면 사용해야한다.

profile
Junior AI Engineer

0개의 댓글