Skip to content

Commit b208c90

Browse files
authored
Create 322.cpp
Added coin change problem
1 parent 7ef4ae8 commit b208c90

File tree

1 file changed

+19
-0
lines changed
  • leetcode/cpp/dynamic programming

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Coin change
2+
3+
class Solution {
4+
public:
5+
int coinChange(vector<int>& coins, int amount) {
6+
vector<int> dp(amount+1, INT_MAX-1);
7+
dp[0] = 0;
8+
9+
for(auto c : coins){
10+
for(int x = c; x<=amount; x++){
11+
dp[x] = min(dp[x], dp[x-c]+1);
12+
}
13+
}
14+
if(dp[amount]==INT_MAX-1){
15+
return -1;
16+
}
17+
return dp[amount];
18+
}
19+
};

0 commit comments

Comments
 (0)