Skip to content

Commit 755ef07

Browse files
authored
Create heozeop.cpp
1 parent 831cfa4 commit 755ef07

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

coin-change/heozeop.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Time Complexity: O(n * amount)
2+
// Spatial Complexity: O(amount)
3+
4+
const int MAX_VALUE = 10001;
5+
const int IMPOSSIBLE = -1;
6+
7+
class Solution {
8+
public:
9+
int coinChange(vector<int>& coins, int amount) {
10+
if (amount == 0) {
11+
return 0;
12+
}
13+
14+
vector<int> dp(amount + 1, MAX_VALUE);
15+
16+
dp[0] = 0;
17+
for(int i = 0; i <= amount; ++i) {
18+
for(int coin : coins) {
19+
if (i < coin) {
20+
continue;
21+
}
22+
23+
if (dp[i - coin] == MAX_VALUE) {
24+
continue;
25+
}
26+
27+
dp[i] = min(1 + dp[i - coin], dp[i]);
28+
}
29+
}
30+
31+
if (dp[amount] == MAX_VALUE) {
32+
return IMPOSSIBLE;
33+
}
34+
35+
return dp[amount];
36+
}
37+
};

0 commit comments

Comments
 (0)