Skip to content

Commit ee86ba8

Browse files
author
github-actions
committed
Format: Google java formatter
1 parent 4ac71cb commit ee86ba8

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

DynamicProgramming/CoinChangeProblem_II/Solution.java

+9-17
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
/**
66
* * Coin Change Problem II (Variation of Unbounded Knapsack Problem)
7-
*
7+
*
88
* Problem: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/
99
*/
10-
1110
public class Solution {
1211
public int minimumNumberOfCoins(int[] coins, int target) {
1312
// return minimumNumberOfCoinsRec(coins.length, coins, target);
@@ -22,29 +21,22 @@ public int minimumNumberOfCoins(int[] coins, int target) {
2221

2322
/**
2423
* * Dynamic Programming Approach
25-
*
24+
*
2625
* * TC: O(nt)
2726
* * SC: O(nt)
2827
*/
2928
private int minimumNumberOfCoinsDP(int[] coins, int target) {
3029
int len = coins.length;
3130
int[][] dp = new int[len + 1][target + 1];
3231

33-
for (int i = 0; i < len + 1; i++)
34-
dp[i][0] = 0;
32+
for (int i = 0; i < len + 1; i++) dp[i][0] = 0;
3533

36-
for (int j = 1; j < target + 1; j++)
37-
dp[0][j] = Integer.MAX_VALUE - 1;
34+
for (int j = 1; j < target + 1; j++) dp[0][j] = Integer.MAX_VALUE - 1;
3835

3936
for (int i = 1; i < len + 1; i++) {
4037
for (int j = 1; j < target + 1; j++) {
41-
if (coins[i - 1] <= j)
42-
dp[i][j] = Math.min(
43-
1 + dp[i][j - coins[i - 1]],
44-
dp[i - 1][j]
45-
);
46-
else
47-
dp[i][j] = dp[i - 1][j];
38+
if (coins[i - 1] <= j) dp[i][j] = Math.min(1 + dp[i][j - coins[i - 1]], dp[i - 1][j]);
39+
else dp[i][j] = dp[i - 1][j];
4840
}
4941
}
5042

@@ -53,7 +45,7 @@ private int minimumNumberOfCoinsDP(int[] coins, int target) {
5345

5446
/**
5547
* * Memoization Approach
56-
*
48+
*
5749
* * TC: O(nt)
5850
* * SC: O(nt)
5951
*/
@@ -75,7 +67,7 @@ private int minimumNumberOfCoinsDP(int[] coins, int target) {
7567

7668
/**
7769
* * Recursive Approach
78-
*
70+
*
7971
* * TC: O(2^n) approximately
8072
* * SC: O(2^n) approximately
8173
*/
@@ -101,4 +93,4 @@ public static void main(String[] args) {
10193
// should be 2
10294
System.out.println(solution.minimumNumberOfCoins(new int[] {9, 6, 5, 1}, 11));
10395
}
104-
}
96+
}

0 commit comments

Comments
 (0)