-
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.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
145 changed files
with
5,327 additions
and
6 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,40 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* ์ฃผ์ด์ง ๋์ ์ ์ข ๋ฅ์ ๊ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฃผ์ด์ง ๊ธ์ก์ ๋ง๋ค ๋, ์ค๋ณต์ ํ์ฉํ ์ต์ ๋์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ | ||
* ๋๋น ์ฐ์ ํ์์ ์ฌ์ฉํด ๋ฌธ์ ํด๊ฒฐ | ||
* ์๊ฐ ๋ณต์ก๋: O(n) | ||
* -> queue ์๋ฃ๊ตฌ์กฐ์์ ๊ฐ ๋์ (n)์ ๊บผ๋ด๊ณ ๋ชฉํ ๊ธ์ก(amount == k)๊น์ง ๋๋ฌํ๋ ๊ฒฝ์ฐ: O(n * k) | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(k) | ||
* -> ๋์ ์ฌ์ฉ ํ์๋ฅผ ์ ์ฅํ๋ board์ ํฌ๊ธฐ | ||
* */ | ||
fun coinChange(coins: IntArray, amount: Int): Int { | ||
if (amount == 0) return 0 | ||
if (coins.isEmpty() || coins.any { it <= 0 }) return -1 | ||
|
||
val board = IntArray(amount + 1) { -1 } | ||
val queue = ArrayDeque<Int>() | ||
|
||
for (coin in coins) { | ||
if (coin <= amount) { | ||
queue.add(coin) | ||
board[coin] = 1 // ๋์ ํ๋๋ก ๊ตฌ์ฑ ๊ฐ๋ฅ | ||
} | ||
} | ||
|
||
while (queue.isNotEmpty()) { | ||
val currentPosition = queue.pollFirst() | ||
for (coin in coins) { | ||
val nextPosition = currentPosition + coin | ||
if (nextPosition in 1..amount) { | ||
// ์์ง ๋ฐฉ๋ฌธํ์ง ์์๊ฑฐ๋ ๋ ์ ์ ๋์ ์ผ๋ก ๊ตฌ์ฑ ๊ฐ๋ฅํ๋ฉด ์ ๋ฐ์ดํธ | ||
if (board[nextPosition] == -1 || board[nextPosition] > board[currentPosition] + 1) { | ||
queue.add(nextPosition) | ||
board[nextPosition] = board[currentPosition] + 1 | ||
} | ||
} | ||
} | ||
} | ||
|
||
return board[amount] | ||
} |
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,31 @@ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
/** | ||
1. understanding | ||
- given coins that can be used, find the minimum count of coins sum up to input amount value. | ||
- [1,2,5]: 11 | ||
- 2 * 5 + 1 * 1: 3 -> use high value coin as much as possible if the remain can be sumed up by remain coins. | ||
2. strategy | ||
- If you search in greedy way, it will takes over O(min(amount/coin) ^ N), given N is the length of coins. | ||
- Let dp[k] is the number of coins which are sum up to amount k, in a given coin set. | ||
- Then, dp[k] = min(dp[k], dp[k-coin] + 1) | ||
3. complexity | ||
- time: O(CA), where C is the length of coins, A is amount value | ||
- space: O(A), where A is amount value | ||
*/ | ||
|
||
int[] dp = new int[amount + 1]; | ||
for (int i = 1; i <= amount; i++) { | ||
dp[i] = amount + 1; | ||
} | ||
|
||
for (int coin: coins) { // O(C) | ||
for (int k = coin; k <= amount; k++) { // O(A) | ||
dp[k] = Math.min(dp[k], dp[k-coin] + 1); | ||
} | ||
} | ||
|
||
return (dp[amount] >= amount + 1) ? -1 : dp[amount]; | ||
} | ||
} | ||
|
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,21 @@ | ||
// ์๊ฐ ๋ณต์ก๋: O(n * m) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* @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 coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
|
||
return dp[amount] === Infinity ? -1 : dp[amount]; | ||
}; | ||
|
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,17 @@ | ||
# space complexity: O(n) (์ฌ๊ธฐ์ n์ amount) | ||
# time complexity: O(n * m) (์ฌ๊ธฐ์ n์ amount, m์ coins์ ๊ธธ์ด) | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [float('inf')] * (amount + 1) | ||
dp[0] = 0 | ||
|
||
for i in range(1, amount + 1): | ||
for coin in coins: | ||
if coin <= i: | ||
dp[i] = min(dp[i], dp[i - coin] + 1) | ||
|
||
return dp[amount] if dp[amount] != float('inf') else -1 | ||
|
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,31 @@ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
|
||
// TC : O(c*a), where c is the number of coins, and a is amount | ||
// SC : O(a) // dp array requires O(a) space | ||
|
||
var coinChange = function (coins, amount) { | ||
// dynamic programming approach | ||
|
||
// dp[amount] : the minimum number of coins | ||
// as a default, dp[0] = 0, for other amounts, dp[amount] = amount + 1 () | ||
// [0, amount+1, amount+1, ...] | ||
const dp = [0, ...new Array(amount).fill(amount + 1)]; | ||
|
||
// start from coin because i - coin >= 0 | ||
for (const coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
// dp[i] : not using the current coin | ||
// dp[i - coin] + 1 : using the current coin | ||
dp[i] = Math.min(dp[i - coin] + 1, dp[i]); | ||
} | ||
} | ||
|
||
// dp[amount] === amount + 1 : that amount of money cannot be made up by any combination of the coins | ||
return dp[amount] < amount + 1 ? dp[amount] : -1; | ||
}; | ||
|
||
|
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,30 @@ | ||
""" | ||
Constraints: | ||
1. 1 <= coins.length <= 12 | ||
2. 1 <= coins[i] <= 2^31 - 1 | ||
3. 0 <= amount <= 10^4 | ||
Time Complexity: O(N*M) | ||
- N์ amount | ||
- M์ ๋์ ์ ๊ฐ์ (coins.length) | ||
Space Complexity: O(N) | ||
- N์ amount | ||
To Do: | ||
- DP ๋ฌธ์ ๋ณต์ตํ๊ธฐ | ||
- Bottom-up๊ณผ Top-down ๋ฐฉ์์ ์ฐจ์ด์ ์ดํดํ๊ธฐ | ||
- ๊ทธ๋ฆฌ๋์ DP์ ์ฐจ์ด์ ๋ณต์ตํ๊ธฐ | ||
""" | ||
|
||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
dp = [sys.maxsize // 2] * (amount + 1) | ||
dp[0] = 0 | ||
|
||
for i in range(1, amount + 1): | ||
for coin in coins: | ||
if i >= coin: | ||
dp[i] = min(dp[i], dp[i-coin] + 1) | ||
|
||
return dp[amount] if dp[amount] != sys.maxsize // 2 else -1 |
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] | ||
} |
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,46 @@ | ||
/** | ||
* ๊ฐ์ง๊ณ ์๋ ๋์ ์ ์ต๋ํ ํ์ฉํ์ฌ ์ต์์ ์กฐํฉ์ผ๋ก amount๋ฅผ ๋ง๋๋ ์ต์ ๋์ ๊ฐ์ ๊ตฌํ๋ ํจ์ | ||
* | ||
* @param {number[]} coins - ์ฌ์ฉ ๊ฐ๋ฅํ ๋์ ๋ฐฐ์ด | ||
* @param {number} amount - ๋ง๋ค์ด์ผ ํ๋ ์ดํฉ | ||
* @returns {number} | ||
* | ||
* ์๊ฐ ๋ณต์ก๋ O(n * m) | ||
* - n์ ๋์ ๋ฐฐ์ด์ ํฌ๊ธฐ | ||
* - m์ amount | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋ (n); | ||
* - ํ์ ์ต๋ n๊ฐ์ ์์๊ฐ ๋ค์ด๊ฐ ์ ์์ | ||
*/ | ||
function coinChange(coins: number[], amount: number): number { | ||
// ์ดํฉ์ด 0์ธ ๊ฒฝ์ฐ 0 ๋ฐํ | ||
if (amount === 0) return 0; | ||
|
||
// ๋๋น ์ฐ์ ํ์์ ํ์ฉํ ํ์ด | ||
|
||
const queue: [number, number] [] = [[0, 0]]; // [ํ์ฌ ์ดํฉ, ๊น์ด] | ||
const visited = new Set<number>(); | ||
|
||
while (queue.length > 0) { | ||
const [currentSum, depth] = queue.shift()!; | ||
|
||
// ๋์ ์ ํ๋์ฉ ๋ํด์ ๋ค์ ๊น์ด์ ํ์ | ||
for (const coin of coins) { | ||
const nextSum = currentSum + coin; | ||
|
||
// ๋ชฉํ ๊ธ์ก์ ๋๋ฌํ๋ฉด ํ์ฌ ๊น์ด๋ฅผ ๋ฐํ | ||
if (nextSum === amount) return depth + 1; | ||
|
||
// ์์ง ์ดํฉ์ ๋๋ฌํ์ง ์์๊ณ , ์ค๋ณต๋์ง ์์ ํ์ ๊ฐ๋ฅํ ๊ฒฝ์ฐ | ||
if (nextSum < amount && !visited.has(nextSum)) { | ||
queue.push([nextSum, depth + 1]); | ||
visited.add(nextSum) | ||
} | ||
|
||
} | ||
} | ||
|
||
// ํ์ ์กฐ๊ฑด์ ์๋ฃ ํด๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ | ||
return -1; | ||
} | ||
|
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,49 @@ | ||
''' | ||
# 322. Coin Change | ||
use a queue for BFS & iterate through the coins and check the amount is down to 0. | ||
use a set to the visited check. | ||
## Time and Space Complexity | ||
``` | ||
TC: O(n * Amount) | ||
SC: O(Amount) | ||
``` | ||
#### TC is O(n * Amount): | ||
- sorting the coins = O(n log n) | ||
- reversing the coins = O(n) | ||
- iterating through the queue = O(Amount) | ||
- iterating through the coins and check the remaining amount is down to 0 = O(n) | ||
#### SC is O(Amount): | ||
- using a queue to store (the remaining amount, the count of coins) tuple = O(Amount) | ||
- using a set to store the visited check = O(Amount) | ||
''' | ||
class Solution: | ||
def coinChange(self, coins: List[int], amount: int) -> int: | ||
if amount == 0: | ||
return 0 | ||
if len(coins) == 1 and coins[0] == amount: | ||
return 1 | ||
|
||
coins.sort() # TC: O(n log n) | ||
coins.reverse() # TC: O(n) | ||
|
||
queue = deque([(amount, 0)]) # SC: O(Amount) | ||
visited = set() # SC: O(Amount) | ||
|
||
while queue: # TC: O(Amount) | ||
remain, count = queue.popleft() | ||
|
||
for coin in coins: # TC: O(n) | ||
next_remain = remain - coin | ||
|
||
if next_remain == 0: | ||
return count + 1 | ||
if next_remain > 0 and next_remain not in visited: | ||
queue.append((next_remain, count + 1)) | ||
visited.add(next_remain) | ||
|
||
return -1 |
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,48 @@ | ||
/* | ||
input : array of integers each element represents coins, single integer amount | ||
output : fewest number of coin to make up the amount | ||
constraint | ||
1) elements are positive? | ||
yes | ||
2) coins are in integer range? | ||
yes | ||
3) range of amoutn | ||
integer range? | ||
[0, 10^4] | ||
4) at least one valid answer exists? | ||
nope. if there no exist than return -1 | ||
edge. case | ||
1) if amount == 0 return 0; | ||
solution 1) top-down approach | ||
amount - each coin | ||
until amount == 0 | ||
return min | ||
tc : O(n * k) when n is amount, k is unique coin numbers | ||
sc : O(n) call stack | ||
solution 2) bottom-up | ||
tc : O(n*k) | ||
sc : O(n) | ||
let dp[i] the minimum number of coins to make up amount i | ||
*/ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
//edge | ||
if(amount == 0) return 0; | ||
int[] dp = new int[amount+1]; | ||
dp[0] = 0; | ||
for(int i = 1; i<= amount; i++) { | ||
dp[i] = amount+1; | ||
for(int coin: coins) { | ||
if(i - coin >= 0) { | ||
dp[i] = Math.min(dp[i], dp[i-coin] + 1); | ||
} | ||
} | ||
} | ||
return dp[amount] == amount+1 ? -1 : dp[amount]; | ||
} | ||
} |
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,28 @@ | ||
/* | ||
Time Complexity: O(coins.length * amount) | ||
Space Complexity: O(amount) | ||
1 ~ i-1์๊น์ง์ ์ต์ ํด๋ฅผ ์๊ณ ์๋ค๋ฉด, i์์ ์ต์ ํด๋ฅผ ๊ตฌํ ๋ coins ๋ฐฐ์ด iteration์ผ๋ก ๊ตฌํ ์ ์์ | ||
*/ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int c = coins.length; | ||
|
||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, 99999); | ||
dp[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (int j = 0; j < c; j++) { | ||
if (i - coins[j] < 0) { | ||
continue; | ||
} | ||
if (dp[i - coins[j]] >= 0 && dp[i - coins[j]] + 1 < dp[i]) { | ||
dp[i] = dp[i - coins[j]] + 1; | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] == 99999 ? -1 : dp[amount]; | ||
} | ||
} |
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,25 @@ | ||
class Solution { | ||
// ์๊ณ ๋ฆฌ์ฆ : dp | ||
/** ํ์ด | ||
* dp๋ฐฐ์ด์ ์ต์ํ์ ๋์ ์ ๊ฐ์๋ฅผ ์ ์ฅ. | ||
* dp[i] = min(dp[i - ๋์ ๊ฐ], dp[i]) ์ค ๋ ์์๊ฐ์ด ์ต์ ๋์ ์ ๊ฐ์. | ||
* */ | ||
// ์๊ฐ : O(coins.len*amount), ๊ณต๊ฐ : O(amount) | ||
fun coinChange(coins: IntArray, amount: Int): Int { | ||
val initValue = Int.MAX_VALUE / 2 | ||
val dp = IntArray(amount + 1) { initValue } | ||
dp[0] = 0 | ||
for (i in 1..amount) { | ||
coins.forEach { c -> | ||
if (c <= i) { | ||
dp[i] = min(dp[i - c] + 1, dp[i]) | ||
} | ||
} | ||
} | ||
return if (dp[amount] == initValue) { | ||
-1 | ||
} else { | ||
dp[amount] | ||
} | ||
} | ||
} |
Oops, something went wrong.