Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaedie authored Jan 7, 2025
2 parents 8bc155f + d52bb73 commit ece6c5e
Show file tree
Hide file tree
Showing 145 changed files with 5,327 additions and 6 deletions.
40 changes: 40 additions & 0 deletions coin-change/EcoFriendlyAppleSu.kt
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]
}
31 changes: 31 additions & 0 deletions coin-change/GangBean.java
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];
}
}

21 changes: 21 additions & 0 deletions coin-change/HerrineKim.js
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];
};

17 changes: 17 additions & 0 deletions coin-change/HodaeSsi.py
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

31 changes: 31 additions & 0 deletions coin-change/Jeehay28.js
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;
};


30 changes: 30 additions & 0 deletions coin-change/KwonNayeon.py
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
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]
}
46 changes: 46 additions & 0 deletions coin-change/Yjason-K.ts
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;
}

49 changes: 49 additions & 0 deletions coin-change/dusunax.py
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
48 changes: 48 additions & 0 deletions coin-change/ekgns33.java
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];
}
}
28 changes: 28 additions & 0 deletions coin-change/forest000014.java
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];
}
}
25 changes: 25 additions & 0 deletions coin-change/gmlwls96.kt
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]
}
}
}
Loading

0 comments on commit ece6c5e

Please sign in to comment.