Skip to content

Commit 186320c

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents d5b1d54 + 09cb2fa commit 186320c

34 files changed

+1222
-4
lines changed

coin-change/Chaedie.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
직접 풀지 못해 알고달레 풀이를 참고했습니다. https://www.algodale.com/problems/coin-change/
3+
4+
Solution:
5+
1) BFS를 통해 모든 동전을 한번씩 넣어보며 amount와 같아지면 return
6+
7+
(c: coins의 종류 갯수, a: amount)
8+
Time: O(ca)
9+
Space: O(a)
10+
"""
11+
12+
13+
class Solution:
14+
def coinChange(self, coins: List[int], amount: int) -> int:
15+
q = deque([(0, 0)]) # (동전 갯수, 누적 금액)
16+
visited = set()
17+
18+
while q:
19+
count, total = q.popleft()
20+
if total == amount:
21+
return count
22+
if total in visited:
23+
continue
24+
visited.add(total)
25+
for coin in coins:
26+
if total + coin <= amount:
27+
q.append((count + 1, total + coin))
28+
return -1

coin-change/gwbaik9717.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// n: amount m: length of coins
2+
// Time complexity: O(n * m) + O(mlogm)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {number[]} coins
7+
* @param {number} amount
8+
* @return {number}
9+
*/
10+
var coinChange = function (coins, amount) {
11+
const dp = Array.from({ length: amount + 1 }, () => Infinity);
12+
dp[0] = 0;
13+
14+
coins.sort((a, b) => a - b);
15+
16+
for (const coin of coins) {
17+
for (let i = coin; i <= amount; i++) {
18+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
19+
}
20+
}
21+
22+
return dp.at(-1) === Infinity ? -1 : dp.at(-1);
23+
};

coin-change/jinah92.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# O(C*A) times, O(A) spaces
2+
class Solution:
3+
def coinChange(self, coins: List[int], amount: int) -> int:
4+
dp = [0] + [amount + 1] * amount
5+
6+
for coin in coins:
7+
for i in range(coin, amount + 1):
8+
dp[i] = min(dp[i], dp[i-coin]+1)
9+
10+
return dp[amount] if dp[amount] < amount + 1 else -1

coin-change/minji-go.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Problem: https://leetcode.com/problems/coin-change/
3+
Description: return the fewest number of coins that you need to make up that amount
4+
Concept: Array, Dynamic Programming, Breadth-First Search
5+
Time Complexity: O(NM), Runtime 15ms - M is the amount
6+
Space Complexity: O(M), Memory 44.28MB
7+
*/
8+
class Solution {
9+
public int coinChange(int[] coins, int amount) {
10+
int[] dp = new int[amount+1];
11+
Arrays.fill(dp, amount+1);
12+
dp[0]=0;
13+
14+
for(int i=1; i<=amount; i++){
15+
for(int coin : coins){
16+
if(i >= coin) {
17+
dp[i] = Math.min(dp[i], dp[i-coin] +1);
18+
}
19+
}
20+
}
21+
return dp[amount]>amount? -1: dp[amount];
22+
}
23+
}

coin-change/mmyeon.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* 접근 방법 :
4+
* - 동전 최소 개수 구하는 문제니까 DP로 풀기
5+
* - 코인마다 순회하면서 동전 개수 최소값 구하기
6+
* - 기존값과 코인을 뺀 값 + 1 중 작은 값으로 업데이트
7+
*
8+
* 시간복잡도 : O(n * m)
9+
* - 코인 개수 n만큼 순회
10+
* - 각 코인마다 amount 값(m) 될 때까지 순회
11+
*
12+
* 공간복잡도 : O(m)
13+
* - amount 만큼 dp 배열 생성
14+
*
15+
*/
16+
function coinChange(coins: number[], amount: number): number {
17+
const dp = Array(amount + 1).fill(Number.MAX_VALUE);
18+
dp[0] = 0;
19+
20+
for (const coin of coins) {
21+
for (let i = coin; i <= amount; i++) {
22+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
23+
}
24+
}
25+
26+
return dp[amount] === Number.MAX_VALUE ? -1 : dp[amount];
27+
}

coin-change/pmjuu.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def coinChange(self, coins: List[int], amount: int) -> int:
6+
# dp[i]: i 금액을 만들기 위해 필요한 최소 동전 개수
7+
dp = [float('inf')] * (amount + 1)
8+
dp[0] = 0
9+
10+
for i in range(1, amount + 1):
11+
for coin in coins:
12+
if coin <= i:
13+
dp[i] = min(dp[i], dp[i - coin] + 1)
14+
15+
return dp[amount] if dp[amount] != float('inf') else -1
16+
17+
18+
# 시간 복잡도:
19+
# - 외부 반복문은 금액(amount)의 범위에 비례하고 -> O(n) (n은 amount)
20+
# - 내부 반복문은 동전의 개수에 비례하므로 -> O(m) (m은 coins의 길이)
21+
# - 총 시간 복잡도: O(n * m)
22+
23+
# 공간 복잡도:
24+
# - dp 배열은 금액(amount)의 크기만큼의 공간을 사용하므로 O(n)
25+
# - 추가 공간 사용은 없으므로 총 공간 복잡도: O(n)

combination-sum/minji-go.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
Problem: https://leetcode.com/problems/combination-sum/
33
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
44
Concept: Array, Backtracking
5-
Time Complexity: O(Nⁿ), Runtime 2ms
6-
Space Complexity: O(N), Memory 44.88MB
7-
8-
- Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :(
5+
Time Complexity: O(Nᵀ), Runtime 2ms
6+
Space Complexity: O(T), Memory 44.88MB
97
*/
108
class Solution {
119
public List<List<Integer>> answer = new ArrayList<>();

merge-two-sorted-lists/Chaedie.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(
8+
self, list1: Optional[ListNode], list2: Optional[ListNode]
9+
) -> Optional[ListNode]:
10+
"""
11+
Solution:
12+
1) 리스트1 리스트2가 null 이 아닌 동안 list1, list2를 차례대로 줄세운다.
13+
2) list1이 남으면 node.next = list1로 남은 리스트를 연결한다.
14+
3) list2가 남으면 list2 를 연결한다.
15+
16+
Time: O(n)
17+
Space: O(1)
18+
"""
19+
dummy = ListNode()
20+
node = dummy
21+
22+
while list1 and list2:
23+
if list1.val < list2.val:
24+
node.next = list1
25+
list1 = list1.next
26+
else:
27+
node.next = list2
28+
list2 = list2.next
29+
node = node.next
30+
31+
if list1:
32+
node.next = list1
33+
elif list2:
34+
node.next = list2
35+
36+
return dummy.next

merge-two-sorted-lists/anniemon.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 시간 복잡도:
3+
* list1의 길이가 m, list2의 길이가 n이면
4+
* 포인터가 최대 m + n만큼 순회하므로 O(m + n)
5+
* 공간 복잡도:
6+
* 포인터를 사용하므로 O(1)
7+
*/
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* @param {ListNode} list1
17+
* @param {ListNode} list2
18+
* @return {ListNode}
19+
*/
20+
var mergeTwoLists = function(list1, list2) {
21+
const head = new ListNode(0, null);
22+
let pointer = head;
23+
while(list1 && list2) {
24+
if(list1.val < list2.val) {
25+
pointer.next = list1;
26+
list1 = list1.next;
27+
} else {
28+
pointer.next = list2;
29+
list2 = list2.next;
30+
}
31+
pointer = pointer.next;
32+
}
33+
if(list1) {
34+
pointer.next = list1;
35+
} else {
36+
pointer.next = list2;
37+
}
38+
return head.next;
39+
};

merge-two-sorted-lists/bus710.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package hello
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
9+
if list1 == nil && list2 == nil {
10+
return nil
11+
} else if list1 == nil {
12+
return list2
13+
} else if list2 == nil {
14+
return list1
15+
}
16+
17+
newList := &ListNode{}
18+
cur := newList
19+
20+
for {
21+
switch {
22+
case list1.Val < list2.Val:
23+
cur.Next = &ListNode{Val: list1.Val}
24+
list1 = list1.Next
25+
case list1.Val > list2.Val:
26+
cur.Next = &ListNode{Val: list2.Val}
27+
list2 = list2.Next
28+
default:
29+
cur.Next = &ListNode{Val: list1.Val}
30+
list1 = list1.Next
31+
cur = cur.Next
32+
cur.Next = &ListNode{Val: list2.Val}
33+
list2 = list2.Next
34+
}
35+
cur = cur.Next
36+
if list1 == nil && list2 == nil && cur.Next == nil {
37+
break
38+
}
39+
40+
if list1 == nil {
41+
cur.Next = list2
42+
break
43+
}
44+
if list2 == nil {
45+
cur.Next = list1
46+
break
47+
}
48+
49+
}
50+
51+
return newList.Next
52+
}

merge-two-sorted-lists/gwbaik9717.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// m: list1, n: list2
2+
// Time complexity: O(m+n)
3+
// Space complexity: O(m+n)
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* function ListNode(val, next) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.next = (next===undefined ? null : next)
10+
* }
11+
*/
12+
/**
13+
* @param {ListNode} list1
14+
* @param {ListNode} list2
15+
* @return {ListNode}
16+
*/
17+
var mergeTwoLists = function (list1, list2) {
18+
const answer = new ListNode();
19+
let current = answer;
20+
21+
while (list1 && list2) {
22+
if (list1.val < list2.val) {
23+
current.next = list1;
24+
list1 = list1.next;
25+
} else {
26+
current.next = list2;
27+
list2 = list2.next;
28+
}
29+
30+
current = current.next;
31+
}
32+
33+
if (list1) {
34+
current.next = list1;
35+
}
36+
37+
if (list2) {
38+
current.next = list2;
39+
}
40+
41+
return answer.next;
42+
};

merge-two-sorted-lists/jinah92.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# O(m+n) times, O(1) spaces
2+
class Solution:
3+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
4+
dummy = ListNode(None)
5+
node = dummy
6+
7+
while list1 and list2:
8+
if list1.val < list2.val:
9+
node.next = list1
10+
list1 = list1.next
11+
else:
12+
node.next = list2
13+
list2 = list2.next
14+
15+
node = node.next
16+
17+
node.next = list1 or list2
18+
return dummy.next

merge-two-sorted-lists/minji-go.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Problem: https://leetcode.com/problems/merge-two-sorted-lists/
3+
Description: return the head of the merged linked list of two sorted linked lists
4+
Concept: Linked List, Recursion
5+
Time Complexity: O(N+M), Runtime 0ms
6+
Space Complexity: O(N+M), Memory 42.74MB
7+
*/
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* public class ListNode {
12+
* int val;
13+
* ListNode next;
14+
* ListNode() {}
15+
* ListNode(int val) { this.val = val; }
16+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
17+
* }
18+
*/
19+
class Solution {
20+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
21+
22+
ListNode head = new ListNode(0);
23+
ListNode tail = head;
24+
25+
while(list1 != null || list2 != null) {
26+
if (list2 == null || (list1 != null && list1.val <= list2.val)) {
27+
tail = tail.next = new ListNode(list1.val);
28+
list1 = list1.next;
29+
} else {
30+
tail = tail.next = new ListNode(list2.val);
31+
list2 = list2.next;
32+
}
33+
}
34+
return head.next;
35+
}
36+
}

0 commit comments

Comments
 (0)