파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 21. 23:45

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

 

Coin Change - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

단순한 DP 문제 중에 하나.
동전 리스트 순서대로 업데이트시켜주면 된다.

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        dp = [math.inf] * (amount + 1)
        dp[0] = 0
        
        for coin in coins:
            for i in range(coin, amount+1):
                if i - coin >= 0:
                    dp[i] = min(dp[i], dp[i-coin] + 1)
        
        return -1 if dp[-1] == math.inf else dp[-1]
반응형

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

Today's Challenge  (0) 2022.05.23
Today's Challenge  (0) 2022.05.22
Today's Challenge  (0) 2022.05.20
Today's Challenge  (0) 2022.05.19
Today's Challenge  (0) 2022.05.18
Comments