-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path322-coin-change.cpp
More file actions
33 lines (32 loc) · 912 Bytes
/
322-coin-change.cpp
File metadata and controls
33 lines (32 loc) · 912 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
29
30
31
32
33
#include <vector>
#include <climits>
using namespace std;
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if (amount == 0) return 0;
int *best = new int[amount+1];
for (int i = 0; i < coins.size(); ++i) {
if (coins[i] <= amount) {
best[coins[i]] = 1;
}
}
best[0] = 0;
for (int i = 1; i <= amount; ++i) {
int min = INT_MAX;
for (int j = 0; j < coins.size(); ++j) {
if (i - coins[j] >= 0 && best[i - coins[j]] >= 0) {
min = (min > 1+best[i-coins[j]]) ? 1+best[i-coins[j]] : min;
}
}
if (min == INT_MAX) {
best[i] = -1;
} else {
best[i] = min;
}
}
int res = best[amount];
delete[] best;
return res;
}
};