Skip to content

Commit 26f1fa6

Browse files
authored
Merge pull request #1055 from Chaedie/main
[Chaedie] Week 12
2 parents af4b350 + 2141633 commit 26f1fa6

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

non-overlapping-intervals/Chaedie.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
Solution: 직접 풀지 못해 해설을 통해 이해하고 풀었습니다.
3+
Interval 문제가 계속 어렵네요.. 😂
4+
Time: O(n)
5+
Space: O(1)
6+
"""
7+
8+
9+
class Solution:
10+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
11+
intervals.sort()
12+
13+
res = 0
14+
prevEnd = intervals[0][1]
15+
for start, end in intervals[1:]:
16+
if start >= prevEnd:
17+
prevEnd = end
18+
else:
19+
res += 1
20+
prevEnd = min(end, prevEnd)
21+
return res
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Solution:
3+
인접 그래프를 그려준다.
4+
dfs 해준다.
5+
중복제거를 위해 visit set 을 사용해야한다.
6+
모든 노드에 대해 dfs 를 하되 갯수를 count 한다.
7+
Time: O(n)
8+
Space: O(n)
9+
"""
10+
11+
12+
class Solution:
13+
def countComponents(self, n: int, edges: List[List[int]]) -> int:
14+
15+
# 인접 리스트
16+
graph = {i: [] for i in range(n)}
17+
for a, b in edges:
18+
graph[a].append(b)
19+
graph[b].append(a)
20+
21+
visit = set()
22+
23+
def dfs(node):
24+
if node in visit:
25+
return
26+
27+
visit.add(node)
28+
for i in graph[node]:
29+
if i not in visit:
30+
dfs(i)
31+
32+
count = 0
33+
for i in range(n):
34+
if i not in visit:
35+
count += 1
36+
dfs(i)
37+
38+
return count
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Solution:
3+
재귀를 통해 끝까지 간뒤 n번째 pop 될때 노드를 삭제해준다.
4+
Time: O(n)
5+
Space: O(n)
6+
"""
7+
8+
9+
class Solution:
10+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
11+
dummy = ListNode()
12+
dummy.next = head
13+
14+
count = 0
15+
16+
def dfs(node, prev):
17+
nonlocal count
18+
if not node:
19+
return
20+
21+
dfs(node.next, node)
22+
count += 1
23+
if count == n:
24+
prev.next = node.next
25+
26+
dfs(head, dummy)
27+
return dummy.next
28+
29+
30+
"""
31+
Solution:
32+
1) 2 Pointer 로 size - n 까지 이동
33+
2) prev 와 curr.next 를 연결
34+
Time: O(n)
35+
Space: O(1)
36+
"""
37+
class Solution:
38+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
39+
40+
dummy = ListNode(0, head)
41+
prev = dummy
42+
curr = tail = head
43+
44+
while n > 0:
45+
tail = tail.next
46+
n -= 1
47+
48+
while tail:
49+
prev = prev.next
50+
curr = curr.next
51+
tail = tail.next
52+
53+
prev.next = curr.next
54+
55+
return dummy.next

same-tree/Chaedie.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Solution: val 이 같고, left, right 이 같으면 된다.
3+
Time: O(n)
4+
Space: O(n)
5+
"""
6+
7+
8+
class Solution:
9+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
10+
11+
if q and p and q.val != p.val:
12+
return False
13+
if not p and not q:
14+
return True
15+
if (not p and q) or (p and not q):
16+
return False
17+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Codec:
2+
"""
3+
Solution: DFS
4+
Time: O(n)
5+
Space: O(n)
6+
"""
7+
8+
def serialize(self, root):
9+
arr = []
10+
11+
def dfs(node):
12+
if not node:
13+
arr.append("null")
14+
return
15+
16+
arr.append(str(node.val))
17+
dfs(node.left)
18+
dfs(node.right)
19+
20+
dfs(root)
21+
22+
return ",".join(arr)
23+
24+
"""
25+
Solution: DFS
26+
Time: O(n)
27+
Space: O(n)
28+
"""
29+
30+
def deserialize(self, data):
31+
vals = data.split(",")
32+
self.i = 0
33+
34+
def dfs():
35+
if vals[self.i] == "null":
36+
self.i += 1
37+
return None
38+
39+
node = TreeNode()
40+
node.val = vals[self.i]
41+
self.i += 1
42+
node.left = dfs()
43+
node.right = dfs()
44+
45+
return node
46+
47+
return dfs()

0 commit comments

Comments
 (0)