-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCoinCombinations.java
More file actions
79 lines (73 loc) · 2.05 KB
/
Copy pathCoinCombinations.java
File metadata and controls
79 lines (73 loc) · 2.05 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.vee.algorithms.dynprog;
import java.util.Arrays;
import java.util.List;
/*
* 2 Problems
*
* Prerequisites - have coins on unlimited quantity of denominations v1, v2, v3 ..
* i. Find mininum number of coins to reach value V
* ii. Fine number of combinations to reach value V
*
*/
public class CoinCombinations {
public static void main(String[] args) {
CoinCombinations c = new CoinCombinations();
System.out.println(c.minNumOfCoins(new int[]{2, 5, 3, 6}, 10));
System.out.println(c.numOfCombinations(new int[]{2, 5, 3, 6}, 10));
System.out.println(c.numOfCombinations2D(new int[]{2, 3, 5}, 5));
}
/*
*
*/
public int minNumOfCoins(int[] coins, int amount) {
int max = amount + 1;
int[] dp = new int[amount + 1];
Arrays.fill(dp, max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
/*
* f(n, k) = 0, k < 1 or n < 0
1, n = 0
f(n, k - 1) + f(n - ak, k). else
*/
public int numOfCombinations(int[] coins, int amount) {
int[] dp = new int[amount + 1];
dp[0] = 1;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
dp[i] = dp[i] + dp[i - coins[j]]; //way to sum upto is count of solutions excluding Jth coin (dp[i]) and including Jth coin (dp[i-coins[j]])
}
}
}
return dp[amount];
}
public int numOfCombinations2D(int[] coins, int amount) {
int[][] dp = new int[amount + 1][coins.length];
// Fill the enteries for 0 value case (n = 0)
for (int i = 0; i < coins.length; i++) {
dp[0][i] = 1;
}
for (int i =1; i < amount+1 ; i++) {
for (int j = 0; j < coins.length; j++) {
// Count of solutions including coins[j]
int x = 0;
if (i - coins[j] >= 0) {
x = dp[i - coins[j]][j];
}
// Count of solutions excluding coins[j]
int y = (j >= 1) ? dp[i][j - 1] : 0;
dp[i][j] = x + y;
}
}
return dp[amount][coins.length-1];
}
}