Skip to content

Commit 12aee53

Browse files
committed
con chnage sol
1 parent df49ade commit 12aee53

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

coin-change/dylan-jung.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
long dp[10001];
4+
int coinChange(vector<int>& coins, int amount) {
5+
long INF = (long)1 << 31;
6+
fill(dp, dp+10001, INF);
7+
8+
for(int& item: coins) {
9+
if(item > 10000) continue;
10+
dp[item] = 1;
11+
}
12+
13+
for(int i = 1; i <= amount; i++) {
14+
for(int& item: coins) {
15+
int j = i - item;
16+
if(j > 0 && dp[j] != -1) {
17+
dp[i] = min(dp[j] + 1, dp[i]);
18+
}
19+
}
20+
}
21+
22+
if(dp[amount] == INF) {
23+
if(amount == 0) return 0;
24+
else return -1;
25+
}
26+
return dp[amount];
27+
}
28+
};

0 commit comments

Comments
 (0)