-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (40 loc) · 1.26 KB
/
Copy pathSolution.java
File metadata and controls
46 lines (40 loc) · 1.26 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
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// amount to fill
int N = in.nextInt();
// number of types of coins
int S = in.nextInt();
int[] vis = new int[S];
for (int i = 0; i < S; i++) {
// values of coins
int vi = in.nextInt();
vis[i] = vi;
}
System.out.println(change(N, vis));
}
// https://hailegia.medium.com/dynamic-programming-number-of-ways-to-make-change-in-5-minutes-fbaf17b8f6f8
// I really dont understand this recurrence, the exercise looks pretty tough to me
public static int change(int amount, int[] coins) {
if (coins == null) {
return 0;
}
int[] F = new int[amount + 1];
F[0] = 1; // This is the base case
for (int coin : coins) {
for (int value = 1; value <= amount; ++value) {
if (coin <= value) {
F[value] += F[value - coin];
}
}
}
return F[amount];
}
}