-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMinimisingCoins.java
68 lines (58 loc) · 1.52 KB
/
MinimisingCoins.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
58
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.util.*;
public class MinimisingCoins {
static int MinimisingCoinsDP(int nums[], int x) {
int n = nums.length;
int ans[] = new int[x+1];
for(int i=0;i<n;i++)
if(nums[i]<x+1)
ans[nums[i]] = 1;
for(int i=0;i<x+1;i++) {
if(ans[i]==1) continue;
ans[i] = (int) 1e7;
for(int j=0;j<n;j++) {
if(i-nums[j]>0)
ans[i] = Math.min(ans[i], 1+ans[i-nums[j]]);
}
}
return ans[x];
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int nums[] = {1,5,7};
int x = 11;
pw.println(MinimisingCoinsDP(nums,x));
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String file) throws FileNotFoundException {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
}