Skip to content

Commit 0053d8a

Browse files
authored
Create (LEETCODE)CoinChange.cpp
1 parent 503f247 commit 0053d8a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int coinChange(vector<int>& coins, int amount)
4+
{
5+
int n=coins.size();
6+
vector<vector<int>>dp(n+1,vector<int>(amount+1));
7+
for(int i=1;i<=n;i++)
8+
{
9+
dp[i][0] = 0;
10+
}
11+
for(int j=0;j<=amount;j++)
12+
{
13+
dp[0][j] = INT_MAX-1;
14+
}
15+
for(int j=1;j<=amount;j++)
16+
{
17+
if(j % coins[0] == 0)
18+
dp[1][j] = j/coins[0];
19+
else
20+
dp[1][j] = INT_MAX-1;
21+
}
22+
for(int i=2;i<=n;i++)
23+
{
24+
for(int j=1;j<=amount;j++)
25+
{
26+
if(coins[i-1]<=j)
27+
dp[i][j] = min(1 + dp[i][j-coins[i-1]] , dp[i-1][j]);
28+
else
29+
dp[i][j] = dp[i-1][j];
30+
}
31+
}
32+
return (dp[n][amount] > amount) ? -1 : dp[n][amount];
33+
34+
}
35+
};

0 commit comments

Comments
 (0)