Skip to content

Commit 0bc0ac8

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 2b9bbc0 + b279188 commit 0bc0ac8

Some content is hidden

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

49 files changed

+2065
-2
lines changed

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/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/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/gmlwls96.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
// 알고리즘 : dp
3+
/** 풀이
4+
* dp배열에 최소한의 동전의 개수를 저장.
5+
* dp[i] = min(dp[i - 동전값], dp[i]) 중 더 작은값이 최소 동전의 개수.
6+
* */
7+
// 시간 : O(coins.len*amount), 공간 : O(amount)
8+
fun coinChange(coins: IntArray, amount: Int): Int {
9+
val initValue = Int.MAX_VALUE / 2
10+
val dp = IntArray(amount + 1) { initValue }
11+
dp[0] = 0
12+
for (i in 1..amount) {
13+
coins.forEach { c ->
14+
if (c <= i) {
15+
dp[i] = min(dp[i - c] + 1, dp[i])
16+
}
17+
}
18+
}
19+
return if (dp[amount] == initValue) {
20+
-1
21+
} else {
22+
dp[amount]
23+
}
24+
}
25+
}

coin-change/imsosleepy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 비슷한 문제를 푼 적이 있어서 쉽게 해결
2+
// https://www.acmicpc.net/problem/2294
3+
// O(N * amount) 시간복잡도가 배열 크기와 amount에 종속된다.
4+
// dp[N]만 사용하므로 공간복잡도는 O(N)
5+
class Solution {
6+
public int coinChange(int[] coins, int amount) {
7+
int[] dp = new int[amount + 1];
8+
Arrays.fill(dp, amount + 1); 불가능한
9+
dp[0] = 0;
10+
11+
for (int i = 1; i <= amount; i++) {
12+
for (int coin : coins) {
13+
if (i >= coin) {
14+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
15+
}
16+
}
17+
}
18+
19+
return dp[amount] > amount ? -1 : dp[amount];
20+
}
21+
}

coin-change/sungjinwi.py

Whitespace-only changes.

coin-change/thispath98.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
"""
4+
Intuition:
5+
dp 배열에 이전 금액에 대한 최소 개수를 저장해두고
6+
갱신하는 방식으로 작동한다.
7+
8+
for 루프를 돌면서 현재 가격에서 coin만큼의 가격을
9+
뺐을 때 거슬러줄 수 있다면, 그 값에서 1개를 더해준
10+
개수를 prev_coins에 저장한다.
11+
12+
이후 prev_coins가 존재하면 현재 인덱스에서 거슬러줄 수 있는
13+
동전의 최소 개수를 갱신한다.
14+
15+
Time Complexity:
16+
O(amount x coins.length):
17+
amount 만큼 루프를 순회하는데 각 루프마다
18+
coins.length 만큼 prev_coins 배열을 만든다.
19+
20+
Space Complexity:
21+
O(amount):
22+
amount만큼의 크기를 가지는 dp 배열을 저장한다.
23+
"""
24+
dp = [0 for _ in range(amount + 1)]
25+
26+
for coin in coins:
27+
if coin <= amount:
28+
dp[coin] = 1
29+
30+
for i in range(1, amount + 1):
31+
if dp[i]:
32+
continue
33+
34+
prev_coins = [dp[i - coin] + 1 for coin in coins if i >= coin and dp[i - coin] > 0]
35+
if prev_coins:
36+
dp[i] = min(prev_coins)
37+
38+
answer = -1 if amount > 0 and dp[amount] == 0 else dp[amount]
39+
return answer
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
/**
14+
1. understanding
15+
- merge 2 sorted linked list
16+
2. strategy
17+
- assign return ListNode
18+
- for each node, started in head, compare each node's value, and add smaller value node to return node, and move the node's head to head.next
19+
3. complexity
20+
- time: O(N + M), N is the length of list1, M is the length of list2
21+
- space: O(1), exclude the return variable
22+
*/
23+
ListNode curr = null;
24+
ListNode ret = null;
25+
26+
while (list1 != null && list2 != null) {
27+
if (list1.val < list2.val) {
28+
ListNode node = new ListNode(list1.val);
29+
if (ret == null) {
30+
ret = node;
31+
} else {
32+
curr.next = node;
33+
}
34+
list1 = list1.next;
35+
curr = node;
36+
} else {
37+
ListNode node = new ListNode(list2.val);
38+
if (ret == null) {
39+
ret = node;
40+
} else {
41+
curr.next = node;
42+
}
43+
list2 = list2.next;
44+
curr = node;
45+
}
46+
}
47+
48+
while (list1 != null) {
49+
ListNode node = new ListNode(list1.val);
50+
if (ret == null) {
51+
ret = node;
52+
} else {
53+
curr.next = node;
54+
}
55+
list1 = list1.next;
56+
curr = node;
57+
}
58+
59+
while (list2 != null) {
60+
ListNode node = new ListNode(list2.val);
61+
if (ret == null) {
62+
ret = node;
63+
} else {
64+
curr.next = node;
65+
}
66+
list2 = list2.next;
67+
curr = node;
68+
}
69+
70+
return ret;
71+
}
72+
}
73+

0 commit comments

Comments
 (0)