-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a18a616
commit 9e3a817
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |