Problem: 322. 零钱兑换
文章目录
- [🎈 思路](#🎈 思路)
- [💖 Code](#💖 Code)
🎈 思路
💖 Code
⏰ 时间复杂度: O ( n ) O(n) O(n)
Java
class Solution {
public int coinChange(int[] coins, int amount)
{
int INF = 0x3f3f3f3f;
int n = amount;
int[] f = new int[n + 1];
Arrays.fill(f, INF);
f[0] = 0;
for (int i = 0; i < coins.length; i++)
{
int x = coins[i];
for (int j = x; j <= n; j++)
f[j] = Math.min(f[j], f[j - x] + 1);
}
if(f[n] == INF)
return -1;
return f[n];
}
}