forked from Nikky01/GeekForGeeks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Number_Of_Coins.java
57 lines (44 loc) · 930 Bytes
/
Number_Of_Coins.java
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
56
57
package com.geek;
import java.util.Scanner;
public class Number_Of_Coins {
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
int v=sc.nextInt();
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(getMinNOCoins(a,n,v));
t--;
}
}
private static int getMinNOCoins(int[] a, int n, int v) {
// TODO Auto-generated method stub
// int res=Integer.MAX_VALUE;
int sub[]=new int[v+1];
sub[0]=0;
for(int i=1;i<=v;i++ )
{
sub[i]=Integer.MAX_VALUE;
}
for(int i=1;i<=v;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]<=i)
{
int sub_res=sub[i-a[j]];
if(sub_res !=Integer.MAX_VALUE&&sub_res+1<sub[i])
sub[i]=sub_res+1;
}
}
}
return sub[v];
}
}