shriyanshi24jindal

Coin change II - min no of coins required to make combination

Mar 28th, 2026
14
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
Java 24.93 KB | None | 0 0
  1. class Solution {
  2. /** Recursion */
  3. public int coinChange(int[] coins, int amount) {
  4. int n = coins.length;
  5.  
  6. int ans = findMinCoins(coins, amount, n);
  7. if(ans == Integer.MAX_VALUE-1)
  8. return -1;
  9. return ans;
  10. }
  11.  
  12. public int findMinCoins(int[] coins, int amount, int n)
  13. {
  14. // base condition: No coins left
  15. // At this point, if amount > 0 (positive), we cannot form the remaining amount, because there are no coins left to use.
  16. // Returning 0 here would mean 0 coins are needed, which is incorrect, because we cannot form the positive amount with no coins.
  17. if(n == 0)
  18. return Integer.MAX_VALUE-1;
  19.  
  20. // 0 coins needed to make amount as 0
  21. if(amount == 0)
  22. return 0;
  23.  
  24. if(coins[n-1] <= amount)
  25. {
  26. // we can pick same coin multiple times
  27. int pick = 1 + findMinCoins(coins, amount-coins[n-1], n);
  28. int notPick = findMinCoins(coins, amount, n-1);
  29. return Math.min(pick, notPick);
  30. }
  31. return findMinCoins(coins, amount, n-1);
  32. }
  33.  
  34. /** Memoization */
  35. public int coinChange(int[] coins, int amount) {
  36. int n = coins.length;
  37.  
  38. int[][] t = new int[n+1][amount+1];
  39.  
  40. // when we have coins, but amount is always 0, so 0 coins needed to make amount as 0
  41. for(int i=0; i<n+1; i++)
  42. {
  43. Arrays.fill(t[i], 0);
  44. }
  45.  
  46. // when we do not have coins, but amount > 0
  47. for(int j=0; j<n+1; j++)
  48. {
  49. Arrays.fill(t[j], Integer.MAX_VALUE-1);
  50. }
  51.  
  52. int ans = findMinCoins(coins, amount, n, t);
  53. if(ans == Integer.MAX_VALUE-1)
  54. return -1;
  55. return ans;
  56. }
  57.  
  58. public int findMinCoins(int[] coins, int amount, int n, int[][] t)
  59. {
  60. // base condition: No coins left
  61. // At this point, if amount > 0 (positive), we cannot form the remaining amount, because there are no coins left to use.
  62. // Returning 0 here would mean 0 coins are needed, which is incorrect, because we cannot form the positive amount with no coins.
  63. if(n == 0)
  64. return Integer.MAX_VALUE-1;
  65.  
  66. // 0 coins needed to make amount as 0
  67. if(amount == 0)
  68. return 0;
  69.  
  70. if(t[n][amount] != Integer.MAX_VALUE - 1)
  71. return t[n][amount];
  72.  
  73. if(coins[n-1] <= amount)
  74. {
  75. // we can pick same coin multiple times
  76. int pick = 1 + findMinCoins(coins, amount-coins[n-1], n, t);
  77. int notPick = findMinCoins(coins, amount, n-1, t);
  78. t[n][amount] = Math.min(pick, notPick);
  79. return t[n][amount];
  80. }
  81. t[n][amount] = findMinCoins(coins, amount, n-1, t);
  82. return t[n][amount];
  83. }
  84.  
  85. /** Top-down approach */
  86. public int coinChange(int[] coins, int amount) {
  87. int n = coins.length;
  88.  
  89. int[][] t = new int[n+1][amount+1];
  90.  
  91. for(int i=0; i<n+1; i++)
  92. {
  93. for(int j=0; j<amount+1; j++)
  94. {
  95. // when coins are 0, we cannot form the amount
  96. if(i == 0)
  97. t[i][j] = Integer.MAX_VALUE - 1;
  98.  
  99. // when amount to be formed is 0, then 0 coins needed
  100. if(j == 0)
  101. t[i][j] = 0;
  102. }
  103. }
  104.  
  105. for(int i=1; i<n+1; i++)
  106. {
  107. for(int j=0; j<amount+1; j++)
  108. {
  109. if(coins[i-1] <= j)
  110. {
  111. // we can pick same coin multiple times
  112. int pick = 1 + t[i][j - coins[i-1]];
  113. int notPick = t[i-1][j];
  114. t[i][j] = Math.min(pick, notPick);
  115. }
  116. else
  117. t[i][j] = t[i-1][j];
  118. }
  119. }
  120.  
  121. int ans = t[n][amount];
  122. if(ans == Integer.MAX_VALUE-1)
  123. return -1;
  124. return ans;
  125. }
  126. }
RAW Paste Data Copied