-
[leetcode 75-17] Coin changeAlgorithm 2023. 1. 30. 23:32
https://leetcode.com/problems/coin-change/
Coin Change - LeetCode
Coin Change - You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money can
leetcode.com
class Solution: def coinChange(self, coins: List[int], amount: int) -> int: dp = [amount+1 for i in range(amount+1)] dp[0] = 0 for i in range(1, amount+1): for j in coins: if i==j: dp[i]=1 break else: if i-j >=0: dp[i] = min(dp[i], 1+dp[i-j]) if dp[amount] == amount+1: return -1 return dp[amount]
'Algorithm' 카테고리의 다른 글
[leetcode 75-19] Number of islands (0) 2023.01.31 [leetcode 75-18] Top K Frequent Elements (0) 2023.01.31 [leetcode 75-16] Climbing Stairs (0) 2023.01.30 [leetcode 75-15] Reverse Bits (0) 2023.01.30 [leetcode 75-14] Missing Number (0) 2023.01.30