forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaysForChange.java
More file actions
48 lines (37 loc) · 859 Bytes
/
WaysForChange.java
File metadata and controls
48 lines (37 loc) · 859 Bytes
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
package algoexpert.medium;
/*
PROBLEM:
Given denominations, find max ways to make amount
LOGIC:
how can we make each amount upto n
n ways
0 -> 1
1 -> 1 (1)
2 -> 1 (1)
..
5 -> 2 (1 or 5)
6 -> 2 (1 or {1,5})
Solution:
for each denom -> calculate ways for each amount
1. time : O(dn) | space : O(n)
*/
public class WaysForChange
{
public static void test()
{
System.out.println(numberOfWaysToMakeChange(6, new int[] {1,5}));
}
public static int numberOfWaysToMakeChange(int n, int[] denoms) {
int [] ways = new int[n+1];
ways[0] = 1;
for (int currency : denoms)
{
for (int amount = 1; amount < n+1; amount++)
{
if (currency <= amount)
{ ways[amount] += ways[amount - currency]; }
}
}
return ways[n];
}
}