Skip to content

Commit

Permalink
feat: coin-change solution
Browse files Browse the repository at this point in the history
  • Loading branch information
YeomChaeeun committed Jan 2, 2025
1 parent a18a616 commit 9e3a817
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions coin-change/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* ๋™์ „๋“ค๋กœ ๊ธˆ์•ก์„ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ๋™์ „์˜ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(nxm) ๋™์ „์˜ ๊ฐœ์ˆ˜ x ๋งŒ๋“ค์–ด์•ผํ•˜๋Š” ๊ธˆ์•ก์˜ ํฌ๊ธฐ
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(m) ์ฃผ์–ด์ง„ ๊ธˆ์•ก์— ๋น„๋ก€ํ•จ
* @param coins
* @param amount
*/
function coinChange(coins: number[], amount: number): number {
const dp = new Array(amount + 1).fill(amount + 1)
dp[0] = 0 // 0์›์€ 0๊ฐœ

for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1)
}
}

return dp[amount] === amount + 1 ? -1 : dp[amount]
}

0 comments on commit 9e3a817

Please sign in to comment.