forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p031.java
32 lines (25 loc) · 911 Bytes
/
p031.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
/*
* Solution to Project Euler problem 31
* By Nayuki Minase
*
* http://nayuki.eigenstate.org/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p031 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p031().run());
}
private static final int TOTAL = 200;
private static int[] COINS = {1, 2, 5, 10, 20, 50, 100, 200};
public String run() {
// Knapsack problem. ways[i][j] is the number of ways to use
// any of the first i coin values to form an unordered sum of j.
int[][] ways = new int[COINS.length + 1][TOTAL + 1];
ways[0][0] = 1;
for (int i = 0; i < COINS.length; i++) {
for (int j = 0; j <= TOTAL; j++)
ways[i + 1][j] = ways[i][j] + (j >= COINS[i] ? ways[i + 1][j - COINS[i]] : 0); // Dynamic programming
}
return Integer.toString(ways[COINS.length][TOTAL]);
}
}