-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoinChange.java
More file actions
112 lines (92 loc) · 3.55 KB
/
Copy pathCoinChange.java
File metadata and controls
112 lines (92 loc) · 3.55 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// You are given coins of different denominations and a total amount of money amount.
// Write a function to compute the fewest number of coins that you need to make up that amount.
// If that amount of money cannot be made up by any combination of the coins, return -1.
// See: https://leetcode.com/problems/coin-change/
package leetcode.dynamic_programming;
import java.util.Arrays;
public class CoinChange {
/**
* Time complexity O(NxM), O(N) space, where N is the target amount.
* Note that for this particular problem the input coins are sorted
* but in the general case the input need to be sorted.
*/
public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
int[] dp = new int[amount + 1];
for (int i = 1; i < dp.length; i++)
dp[i] = amount + 1;
for (int i = 0; i < coins.length; i++) {
for (int j = 1; j < dp.length; j++) {
if (j >= coins[i]) {
dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
}
}
}
return dp[amount] <= amount ? dp[amount] : -1;
}
/**
* Time complexity O(NxM), O(NxM) space.
*/
public int coinChange2(int[] coins, int amount) {
Arrays.sort(coins);
int n = coins.length + 1;
int m = amount + 1;
int[][] dp = new int[n][m];
for (int j = 1; j < m; j++)
dp[0][j] = amount + 1;
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
if (j < coins[i - 1]) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - coins[i - 1]] + 1);
}
return dp[n - 1][m - 1] <= amount ? dp[n - 1][m - 1] : -1;
}
/**
* Good idea but very ugly implementation
*/
public int coinChange1(int[] coins, int amount) {
if (amount == 0)
return 0;
Arrays.sort(coins);
int n = coins.length;
int m = amount + 1;
int[][] dp = new int[n][m];
for (int j = 1; j < dp[0].length; j++) {
if (j >= coins[0] && j % coins[0] == 0) {
dp[0][j] = j / coins[0];
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
if (coins[i] == j) {
dp[i][j] = 1;
} else if (j >= coins[i] && dp[i - 1][j] == 0 && dp[i][j - coins[i]] > 0) {
dp[i][j] = dp[i][j - coins[i]] + 1;
} else if (j < coins[i] || dp[i][j - coins[i]] == 0) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - coins[i]] + 1);
}
}
}
// printTable(dp);
return dp[n - 1][m - 1] != 0 ? dp[n - 1][m - 1] : -1;
}
public static void main(String[] args) {
CoinChange sln = new CoinChange();
System.out.println(sln.coinChange(new int[] { 4, 5 }, 6));
System.out.println(sln.coinChange(new int[] { 1, 3, 5 }, 10));
System.out.println(sln.coinChange(new int[] { 2 }, 3));
System.out.println(sln.coinChange(new int[] { 1 }, 0));
System.out.println(sln.coinChange(new int[] { 3, 4, 5 }, 8));
System.out.println(sln.coinChange(new int[] { 186, 419, 83, 408 }, 6249));
}
@SuppressWarnings("unused")
private void printTable(int[][] dp) {
for (int[] line : dp) {
System.out.println(Arrays.toString(line));
}
}
}