본문 바로가기
문제풀이

Leetcode Coin Change Java

by AndoneKwon 2021. 1. 20.
class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];
        Arrays.fill(dp,amount+1);
        dp[0]=0;
        int total=0;

        if(amount==0)
            return 0;

        for(int i=0; i<=amount; i++) {
            for(int money : coins) {
                if(money <= i){
                    dp[i]=Math.min(dp[i],dp[i-money]+1);
                }
            }
        }
        if(dp[amount]==amount+1){
            return -1;
        }
        return dp[amount];
    }
}

'문제풀이' 카테고리의 다른 글

프로그래머스 모의고사 Java  (0) 2021.03.24
프로그래머스 K번째 수 Java  (0) 2021.03.24
Leetcode Count and Say Java  (0) 2021.01.20
Leetcode MinStack JAVA  (0) 2021.01.05
Leetcode Climbing Stairs JAVA  (0) 2021.01.04