-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMInimumNumberOfCoins.java
More file actions
53 lines (42 loc) · 1.3 KB
/
MInimumNumberOfCoins.java
File metadata and controls
53 lines (42 loc) · 1.3 KB
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
// Geeks for Geeks version. Values print in order when they should print in reverse order, otherwise correct.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int[] coins = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
for(int c = 0; c < T; c++){
int r = Integer.parseInt(br.readLine());
int[] ans = new int[r+1];
int[] pointers = new int[r+1];
Arrays.fill(ans,r+1);
ans[0] = 0;
for(int i = 1 ; i < r+1; i++){
for(int j = 0; j < coins.length; j++){
if(i - coins[j] < 0){
continue;
}
if(1 + ans[i - coins[j]] < ans[i]){
ans[i] = 1 + ans[i - coins[j]];
pointers[i] = j;
}
}
}
if(ans[r] == r+1){
System.out.println("");
continue;
}
int k = r;
while(k > 0){
System.out.print(coins[pointers[k]]);
k -= coins[pointers[k]];
if(k > 0){
System.out.print(" ");
}
}
System.out.println("");
}
}
}