-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path322.cpp
38 lines (33 loc) · 923 Bytes
/
322.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int m=coins.size();
sort(coins.begin(),coins.end());
long long int A[m+1][amount+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=amount;j++){
if(i==0 && j==0){
A[i][j]=0;
}
else if(i==0){
A[i][j]=INT_MAX;
}
else if(j==0){
A[i][j]=0;
}
else if(j<coins[i-1]) {
A[i][j]=A[i-1][j];
}
else{
A[i][j]=min( A[i-1][j] , (A[i][j-coins[i-1]]+1)) ;
}
}
}
if( A[m][amount]==INT_MAX){
return -1;
}
else {
return A[m][amount];
}
}
};