Skip to content

Commit ece6c5e

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 8bc155f + d52bb73 commit ece6c5e

File tree

145 files changed

+5327
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+5327
-6
lines changed

coin-change/EcoFriendlyAppleSu.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode_study
2+
3+
/*
4+
* 주어진 동전의 종류와 개수를 사용하여 주어진 금액을 만들 때, 중복을 허용한 최소 동전 개수를 구하는 문제
5+
* 너비 우선 탐색을 사용해 문제 해결
6+
* 시간 복잡도: O(n)
7+
* -> queue 자료구조에서 각 동전(n)을 꺼내고 목표 금액(amount == k)까지 도달하는 경우: O(n * k)
8+
* 공간 복잡도: O(k)
9+
* -> 동전 사용 횟수를 저장하는 board의 크기
10+
* */
11+
fun coinChange(coins: IntArray, amount: Int): Int {
12+
if (amount == 0) return 0
13+
if (coins.isEmpty() || coins.any { it <= 0 }) return -1
14+
15+
val board = IntArray(amount + 1) { -1 }
16+
val queue = ArrayDeque<Int>()
17+
18+
for (coin in coins) {
19+
if (coin <= amount) {
20+
queue.add(coin)
21+
board[coin] = 1 // 동전 하나로 구성 가능
22+
}
23+
}
24+
25+
while (queue.isNotEmpty()) {
26+
val currentPosition = queue.pollFirst()
27+
for (coin in coins) {
28+
val nextPosition = currentPosition + coin
29+
if (nextPosition in 1..amount) {
30+
// 아직 방문하지 않았거나 더 적은 동전으로 구성 가능하면 업데이트
31+
if (board[nextPosition] == -1 || board[nextPosition] > board[currentPosition] + 1) {
32+
queue.add(nextPosition)
33+
board[nextPosition] = board[currentPosition] + 1
34+
}
35+
}
36+
}
37+
}
38+
39+
return board[amount]
40+
}

coin-change/GangBean.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int coinChange(int[] coins, int amount) {
3+
/**
4+
1. understanding
5+
- given coins that can be used, find the minimum count of coins sum up to input amount value.
6+
- [1,2,5]: 11
7+
- 2 * 5 + 1 * 1: 3 -> use high value coin as much as possible if the remain can be sumed up by remain coins.
8+
2. strategy
9+
- If you search in greedy way, it will takes over O(min(amount/coin) ^ N), given N is the length of coins.
10+
- Let dp[k] is the number of coins which are sum up to amount k, in a given coin set.
11+
- Then, dp[k] = min(dp[k], dp[k-coin] + 1)
12+
3. complexity
13+
- time: O(CA), where C is the length of coins, A is amount value
14+
- space: O(A), where A is amount value
15+
*/
16+
17+
int[] dp = new int[amount + 1];
18+
for (int i = 1; i <= amount; i++) {
19+
dp[i] = amount + 1;
20+
}
21+
22+
for (int coin: coins) { // O(C)
23+
for (int k = coin; k <= amount; k++) { // O(A)
24+
dp[k] = Math.min(dp[k], dp[k-coin] + 1);
25+
}
26+
}
27+
28+
return (dp[amount] >= amount + 1) ? -1 : dp[amount];
29+
}
30+
}
31+

coin-change/HerrineKim.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 시간 복잡도: O(n * m)
2+
// 공간 복잡도: O(n)
3+
4+
/**
5+
* @param {number[]} coins
6+
* @param {number} amount
7+
* @return {number}
8+
*/
9+
var coinChange = function(coins, amount) {
10+
const dp = new Array(amount + 1).fill(Infinity);
11+
dp[0] = 0;
12+
13+
for (let 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] === Infinity ? -1 : dp[amount];
20+
};
21+

coin-change/HodaeSsi.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# space complexity: O(n) (여기서 n은 amount)
2+
# time complexity: O(n * m) (여기서 n은 amount, m은 coins의 길이)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def coinChange(self, coins: List[int], amount: int) -> int:
8+
dp = [float('inf')] * (amount + 1)
9+
dp[0] = 0
10+
11+
for i in range(1, amount + 1):
12+
for coin in coins:
13+
if coin <= i:
14+
dp[i] = min(dp[i], dp[i - coin] + 1)
15+
16+
return dp[amount] if dp[amount] != float('inf') else -1
17+

coin-change/Jeehay28.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
7+
// TC : O(c*a), where c is the number of coins, and a is amount
8+
// SC : O(a) // dp array requires O(a) space
9+
10+
var coinChange = function (coins, amount) {
11+
// dynamic programming approach
12+
13+
// dp[amount] : the minimum number of coins
14+
// as a default, dp[0] = 0, for other amounts, dp[amount] = amount + 1 ()
15+
// [0, amount+1, amount+1, ...]
16+
const dp = [0, ...new Array(amount).fill(amount + 1)];
17+
18+
// start from coin because i - coin >= 0
19+
for (const coin of coins) {
20+
for (let i = coin; i <= amount; i++) {
21+
// dp[i] : not using the current coin
22+
// dp[i - coin] + 1 : using the current coin
23+
dp[i] = Math.min(dp[i - coin] + 1, dp[i]);
24+
}
25+
}
26+
27+
// dp[amount] === amount + 1 : that amount of money cannot be made up by any combination of the coins
28+
return dp[amount] < amount + 1 ? dp[amount] : -1;
29+
};
30+
31+

coin-change/KwonNayeon.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Constraints:
3+
1. 1 <= coins.length <= 12
4+
2. 1 <= coins[i] <= 2^31 - 1
5+
3. 0 <= amount <= 10^4
6+
7+
Time Complexity: O(N*M)
8+
- N은 amount
9+
- M은 동전의 개수 (coins.length)
10+
11+
Space Complexity: O(N)
12+
- N은 amount
13+
14+
To Do:
15+
- DP 문제 복습하기
16+
- Bottom-up과 Top-down 방식의 차이점 이해하기
17+
- 그리디와 DP의 차이점 복습하기
18+
"""
19+
20+
class Solution:
21+
def coinChange(self, coins: List[int], amount: int) -> int:
22+
dp = [sys.maxsize // 2] * (amount + 1)
23+
dp[0] = 0
24+
25+
for i in range(1, amount + 1):
26+
for coin in coins:
27+
if i >= coin:
28+
dp[i] = min(dp[i], dp[i-coin] + 1)
29+
30+
return dp[amount] if dp[amount] != sys.maxsize // 2 else -1

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+
}

coin-change/Yjason-K.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 가지고 있는 동전을 최대한 활용하여 최소의 조합으로 amount를 만드는 최소 동전 개수 구하는 함수
3+
*
4+
* @param {number[]} coins - 사용 가능한 동전 배열
5+
* @param {number} amount - 만들어야 하는 총합
6+
* @returns {number}
7+
*
8+
* 시간 복잡도 O(n * m)
9+
* - n은 동전 배열의 크기
10+
* - m은 amount
11+
*
12+
* 공간 복잡도 (n);
13+
* - 큐에 최대 n개의 요소가 들어갈 수 있음
14+
*/
15+
function coinChange(coins: number[], amount: number): number {
16+
// 총합이 0인 경우 0 반환
17+
if (amount === 0) return 0;
18+
19+
// 너비 우선 탐색을 활용한 풀이
20+
21+
const queue: [number, number] [] = [[0, 0]]; // [현재 총합, 깊이]
22+
const visited = new Set<number>();
23+
24+
while (queue.length > 0) {
25+
const [currentSum, depth] = queue.shift()!;
26+
27+
// 동전을 하나씩 더해서 다음 깊이을 탐색
28+
for (const coin of coins) {
29+
const nextSum = currentSum + coin;
30+
31+
// 목표 금액에 도달하면 현재 깊이를 반환
32+
if (nextSum === amount) return depth + 1;
33+
34+
// 아직 총합에 도달하지 않았고, 중복되지 않아 탐색 가능한 경우
35+
if (nextSum < amount && !visited.has(nextSum)) {
36+
queue.push([nextSum, depth + 1]);
37+
visited.add(nextSum)
38+
}
39+
40+
}
41+
}
42+
43+
// 탐색 조건을 완료 해도 경우의 수를 찾지 못한 경우
44+
return -1;
45+
}
46+

coin-change/dusunax.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
# 322. Coin Change
3+
4+
use a queue for BFS & iterate through the coins and check the amount is down to 0.
5+
use a set to the visited check.
6+
7+
## Time and Space Complexity
8+
9+
```
10+
TC: O(n * Amount)
11+
SC: O(Amount)
12+
```
13+
14+
#### TC is O(n * Amount):
15+
- sorting the coins = O(n log n)
16+
- reversing the coins = O(n)
17+
- iterating through the queue = O(Amount)
18+
- iterating through the coins and check the remaining amount is down to 0 = O(n)
19+
20+
#### SC is O(Amount):
21+
- using a queue to store (the remaining amount, the count of coins) tuple = O(Amount)
22+
- using a set to store the visited check = O(Amount)
23+
'''
24+
class Solution:
25+
def coinChange(self, coins: List[int], amount: int) -> int:
26+
if amount == 0:
27+
return 0
28+
if len(coins) == 1 and coins[0] == amount:
29+
return 1
30+
31+
coins.sort() # TC: O(n log n)
32+
coins.reverse() # TC: O(n)
33+
34+
queue = deque([(amount, 0)]) # SC: O(Amount)
35+
visited = set() # SC: O(Amount)
36+
37+
while queue: # TC: O(Amount)
38+
remain, count = queue.popleft()
39+
40+
for coin in coins: # TC: O(n)
41+
next_remain = remain - coin
42+
43+
if next_remain == 0:
44+
return count + 1
45+
if next_remain > 0 and next_remain not in visited:
46+
queue.append((next_remain, count + 1))
47+
visited.add(next_remain)
48+
49+
return -1

coin-change/ekgns33.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
input : array of integers each element represents coins, single integer amount
3+
output : fewest number of coin to make up the amount
4+
constraint
5+
1) elements are positive?
6+
yes
7+
2) coins are in integer range?
8+
yes
9+
3) range of amoutn
10+
integer range?
11+
[0, 10^4]
12+
4) at least one valid answer exists?
13+
nope. if there no exist than return -1
14+
15+
edge. case
16+
1) if amount == 0 return 0;
17+
18+
solution 1) top-down approach
19+
20+
amount - each coin
21+
until amount == 0
22+
return min
23+
tc : O(n * k) when n is amount, k is unique coin numbers
24+
sc : O(n) call stack
25+
26+
solution 2) bottom-up
27+
tc : O(n*k)
28+
sc : O(n)
29+
let dp[i] the minimum number of coins to make up amount i
30+
31+
*/
32+
class Solution {
33+
public int coinChange(int[] coins, int amount) {
34+
//edge
35+
if(amount == 0) return 0;
36+
int[] dp = new int[amount+1];
37+
dp[0] = 0;
38+
for(int i = 1; i<= amount; i++) {
39+
dp[i] = amount+1;
40+
for(int coin: coins) {
41+
if(i - coin >= 0) {
42+
dp[i] = Math.min(dp[i], dp[i-coin] + 1);
43+
}
44+
}
45+
}
46+
return dp[amount] == amount+1 ? -1 : dp[amount];
47+
}
48+
}

0 commit comments

Comments
 (0)