Skip to content

Commit 821cd6f

Browse files
committed
_322.cpp added (Coin change DP solution)
1 parent 9418c21 commit 821cd6f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cpp/_322.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// coin-change
2+
// Problem Statement: https://leetcode.com/problems/coin-change/
3+
4+
class Solution{
5+
public:
6+
int coinChange(vector<int>& coins, int amount){
7+
8+
int MAX = amount + 1;
9+
vector<int> cache(amount + 1, MAX);
10+
11+
cache[0] = 0;
12+
for(auto coin : coins){
13+
for(int i = coin; i <= amount; i++)
14+
cache[i] = std::min(cache[i], cache[i - coin] + 1);
15+
}
16+
17+
return cache[amount] == MAX ? -1 : cache[amount];
18+
}
19+
};
20+

0 commit comments

Comments
 (0)