λ¬Έμ
νμ΄ κ³Όμ
μ€λλ§μ νμ΄λ³΄λ λμ κ³νλ²
λ¬Έμ μ
λλ€. dp[x] = x μμ λ§λ€κΈ° μν μ΅μ λμ μ κ°μ
λΌκ³ μ μνλ€λ©΄ λ€μκ³Ό κ°μ μ νμμ μ»μ μ μμ΅λλ€.
dp[x] = for all coins, min{ dp[x - coins[i]] }
μ΄λ i
λ i
λ²μ§Έ λμ μ μλ―Έν©λλ€.
μ½λ
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function (coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
// μ΄κΈ°κ°
dp[0] = 0;
for (let money = 1; money <= amount; money++) {
for (let i = 0; i < coins.length; i++) {
if (coins[i] <= money) {
dp[money] = Math.min(dp[money], dp[money - coins[i]] + 1);
}
}
}
const answer = dp[amount] === Infinity ? -1 : dp[amount];
return answer;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 287 - Find the Duplicate Number (Medium) (0) | 2021.03.03 |
---|---|
LeetCode 347 - Top K Frequent Elements (Medium) (0) | 2021.03.03 |
LeetCode 46 - Permutations (Medium) (0) | 2021.03.03 |
LeetCode 21 - Merge Two Sorted Lists (Easy) (0) | 2021.03.02 |
LeetCode 240 - Search a 2D Matrix II (Medium) (0) | 2021.03.02 |
π¬ λκΈ