-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path322.cpp
More file actions
28 lines (26 loc) · 781 Bytes
/
Copy path322.cpp
File metadata and controls
28 lines (26 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int len = coins.size();
int dp[20001];
dp[0] = 0;
for(int i = 1; i <= amount; ++i)dp[i] = -1;
for(int i = 0; i < len; ++i)
{
//for(int j = amount; j >= coins[i]; --j)
for(int j = coins[i]; j <= amount; ++j)
{
//for(int k = 1; j-k*coins[i] >= 0; ++k)
//if(dp[j-coins[i]*k] != -1)
if(dp[j-coins[i]] != -1)
{
if(dp[j] == -1 || dp[j-coins[i]] + 1 < dp[j])
{
dp[j] = dp[j-coins[i]] + 1;
}
}
}
}
return dp[amount];
}
};