1. ๋ฌธ์ œ : https://leetcode.com/problems/coin-change/

๋™์ „์„ ์ตœ์†Œ๋กœ ์‚ฌ์šฉํ•ด ํŠน์ • ์ˆซ์ž๋ฅผ ๋งŒ๋“ค๊ธฐ (์ค‘๋ณต์‚ฌ์šฉ ๊ฐ€๋Šฅ)

 

2. ํ’€์ด

dp ๋ฌธ์ œ

๋™์ „ ์ตœ์†Œํฌ๊ธฐ์ธ 11๋ณด๋‹ค +1์ˆ˜๋ฅผ max๊ฐ’์œผ๋กœ dp์— ์ €์žฅํ•˜๊ณ ,

ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ์‚ฌ์šฉํšŸ์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹ (์–ด๋ ค์›€...)

 

ex)

์ˆซ์ž 2๋Š” ์ฝ”์ธ2 ํ•œ๋ฒˆ์— ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ -> ์ด๊ฑธ ์–ด๋–ป๊ฒŒ dp๋กœ ์ž๋™์œผ๋กœ ํ•˜๋ƒ? -> dp[i-money์ฆ‰ 0]  +1 ๊ณผ dp[i] ๊ฐ’ ์ค‘ ๋น„๊ตํ•ด์„œ ๋„ฃ์œผ๋ฉด๋จ

 

3. ์ฝ”๋“œ

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

        if(amount==0)
            return 0;

        for(int i=1; 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];
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ