파비의 매일매일 공부기록

2023.08.11 Today's Challenge 본문

Problem Solving/LeetCode

2023.08.11 Today's Challenge

fabichoi 2023. 8. 11. 23:45

https://leetcode.com/problems/coin-change-ii/

 

Coin Change II - LeetCode

Can you solve this real interview question? Coin Change II - You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the number of combinations that make up that

leetcode.com

완전 탐색으로 푸는 문제!

class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        def solve(i, amount):
            if amount == 0:
                return 1
            if i == len(coins):
                return 0
            if memo[i][amount] != -1:
                return memo[i][amount]

            if coins[i] > amount:
                memo[i][amount] = solve(i+1, amount)
            else:
                memo[i][amount] = solve(i, amount-coins[i]) + solve(i+1, amount)
        
            return memo[i][amount]
        
        memo = [[-1] * (amount + 1) for _ in range(len(coins))]
        return solve(0, amount)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.08.13 Today's Challenge  (0) 2023.08.13
2023.08.12 Today's Challenge  (0) 2023.08.12
2023.08.10 Today's Challenge  (0) 2023.08.10
2023.08.09 Today's Challenge  (0) 2023.08.09
2023.08.08 Today's Challenge  (0) 2023.08.08
Comments