-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion18.c
55 lines (48 loc) · 804 Bytes
/
question18.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Min coin
http://practice.geeksforgeeks.org/problems/min-coin/0
*/
/*
Min coin
http://practice.geeksforgeeks.org/problems/min-coin/0
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int minCoins(int *arr, int n, int amount){
int dp[amount+1];
dp[0] = 0;
int i,j;
for(i=1;i<=amount;i++){
dp[i] = 2000;
}
for(j=0;j<n;j++){
for(i=1;i<=amount;i++){
if(i >= arr[j]){
if(dp[i-arr[j]] + 1 < dp[i]){
dp[i] = dp[i-arr[j]] + 1;
}
}
}
}
if(dp[amount] == 2000){
return -1;
}
return dp[amount];
}
int main(){
int cases;
scanf("%d", &cases);
int i;
for(i=0;i<cases;i++){
int n, amount;
scanf("%d %d",&n, &amount);
int arr[n];
int j;
for(j=0;j<n;j++){
scanf("%d", &arr[j]);
}
printf("%d\n", minCoins(arr,n,amount));
}
return 0;
}