Skip to content

Commit 12117a4

Browse files
authored
[ PS ] : Coin Change
1 parent 77ed47a commit 12117a4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

coin-change/uraflower.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 주어진 금액을 동전으로 거스를 때 최소 동전 개수를 반환하는 함수
3+
* @param {number[]} coins
4+
* @param {number} amount
5+
* @return {number}
6+
*/
7+
const coinChange = function (coins, amount) {
8+
const dp = [0, ...Array(amount).fill(amount + 1)];
9+
10+
for (let coin of coins) {
11+
for (let n = coin; n <= amount; n++) {
12+
dp[n] = Math.min(dp[n], dp[n - coin] + 1)
13+
}
14+
}
15+
16+
return dp[amount] > amount ? -1 : dp[amount]
17+
};
18+
19+
// 시간복잡도: O(c*n) (c: coins.length, n: amount)
20+
// 공간복잡도: O(n)

0 commit comments

Comments
 (0)