Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 705 Bytes

Coin Change 2.md

File metadata and controls

33 lines (24 loc) · 705 Bytes

Algorithm

Video explanation

Solution

class Solution {
    public int change(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        for (int coin : coins) {
            for (int i = coin; i <= amount; i++) {
                dp[i] += dp[i-coin];
            }
        }
        return dp[amount];
    }
}

Notes

  • In the general case, our solution may overflow our int return value. Can use a long instead.

Time/Space Complexity

  • Time Complexity: O(amount * coins)
  • Space Complexity: O(amount)

Links