classSolution{public:intcoinChange(vector<int>&coins,intamount){// dp[i] := fewest # of coins to make up ivector<int>dp(amount+1,amount+1);dp[0]=0;for(constintcoin:coins)for(inti=coin;i<=amount;++i)dp[i]=min(dp[i],dp[i-coin]+1);returndp[amount]==amount+1?-1:dp[amount];}};
classSolution{publicintcoinChange(int[]coins,intamount){// dp[i] := fewest # of coins to make up iint[]dp=newint[amount+1];Arrays.fill(dp,1,dp.length,amount+1);for(finalintcoin:coins)for(inti=coin;i<=amount;++i)dp[i]=Math.min(dp[i],dp[i-coin]+1);returndp[amount]==amount+1?-1:dp[amount];}}
classSolution:defcoinChange(self,coins:List[int],amount:int)->int:# dp[i] := fewest # Of coins to make up idp=[0]+[amount+1]*amountforcoinincoins:foriinrange(coin,amount+1):dp[i]=min(dp[i],dp[i-coin]+1)return-1ifdp[amount]==amount+1elsedp[amount]