leetcode 零钱兑换 中等
作者:互联网
发现 coins.length 很小,而且 amount 最大为 1e4,所以完全背包 dp 即可。
像这个数一样采用 BFS 也行,但是很慢。https://leetcode-cn.com/problems/perfect-squares/
class Solution { public: int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, 0x3f3f3f3f); dp[0] = 0; for(int i = 0; i <= amount; ++ i) { for(int j = 0; j < coins.size(); ++ j) { if(i >= coins[j]) dp[i] = min(dp[i], dp[i - coins[j]] + 1); } } return dp.back() == 0x3f3f3f3f ? -1 : dp.back(); } };
标签:int,coins,back,零钱,amount,兑换,0x3f3f3f3f,leetcode,dp 来源: https://www.cnblogs.com/rookie-acmer/p/15159162.html