Skip to content

Commit 9e3a817

Browse files
committed
feat: coin-change solution
1 parent a18a616 commit 9e3a817

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

โ€Žcoin-change/YeomChaeeun.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* ๋™์ „๋“ค๋กœ ๊ธˆ์•ก์„ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ๋™์ „์˜ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(nxm) ๋™์ „์˜ ๊ฐœ์ˆ˜ x ๋งŒ๋“ค์–ด์•ผํ•˜๋Š” ๊ธˆ์•ก์˜ ํฌ๊ธฐ
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(m) ์ฃผ์–ด์ง„ ๊ธˆ์•ก์— ๋น„๋ก€ํ•จ
6+
* @param coins
7+
* @param amount
8+
*/
9+
function coinChange(coins: number[], amount: number): number {
10+
const dp = new Array(amount + 1).fill(amount + 1)
11+
dp[0] = 0 // 0์›์€ 0๊ฐœ
12+
13+
for (const coin of coins) {
14+
for (let i = coin; i <= amount; i++) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1)
16+
}
17+
}
18+
19+
return dp[amount] === amount + 1 ? -1 : dp[amount]
20+
}

0 commit comments

Comments
ย (0)