Advertisement
aero2146

Coin change

Dec 30th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. class Solution {
  2.     public int coinChange(int[] coins, int amount) {
  3.         int[] dp = new int[amount+1];
  4.         Arrays.fill(dp, -1);
  5.        
  6.         // 0 ways of making $0
  7.         dp[0] = 0;
  8.  
  9.         for (int i = 1; i < amount+1; i++) {
  10.             for (int c = 0; c < coins.length; c++) {
  11.                 if (i-coins[c] >= 0 && dp[i-coins[c]] != -1) {
  12.                     dp[i] = (dp[i] == -1) ? dp[i-coins[c]]+1 : Math.min(dp[i], dp[i-coins[c]]+1);
  13.                 }
  14.             }
  15.         }
  16.  
  17.         return dp[amount];
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement