Skip to content

Commit 1c0bddb

Browse files
authored
Merge pull request #2307 from Seoya0512/main
[Seoya0512] WEEK12 Solutions
2 parents 0e03c40 + 0dbd98a commit 1c0bddb

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
Time Complexity: O(n log n)
3+
- intervals ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋Š”๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„ (n์€ intervals์˜ ๊ธธ์ด)
4+
- ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์„ ์ฐพ๋Š”๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„ O(n)์ด๋‚˜ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ
5+
6+
# Space Complexity: O(1)
7+
- last_end, count ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๊ณต๊ฐ„
8+
'''
9+
10+
11+
class Solution:
12+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
13+
intervals.sort(key=lambda x: x[1])
14+
last_end = intervals[0][1]
15+
count = 0
16+
17+
for val in intervals[1:]:
18+
if (val[0] < last_end):
19+
print(val)
20+
count += 1
21+
if (val[0] >= last_end):
22+
last_end = val[1]
23+
return count
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Time Complexity: O(N + E)
3+
- ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E
4+
- ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ„์„ ์˜ ์ˆ˜๋ฅผ ํ•ฉ์นœ๋งŒํผ ์žฌ๊ท€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ•„์š”
5+
6+
Space Complexity: O(N + E)
7+
- ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ ๋…ธ๋“œ์™€ ๊ฐ„์„ ์˜ ์ˆ˜์˜ ํ•ฉ์— ๋น„๋ก€
8+
- ์ง‘ํ•ฉ์— ์ตœ๋Œ€ N๊ฐœ์˜ ์ˆซ์ž ์ €์žฅ
9+
'''
10+
from typing import (
11+
List,
12+
)
13+
14+
class Solution:
15+
"""
16+
@param n: the number of vertices
17+
@param edges: the edges of undirected graph
18+
@return: the number of connected components
19+
"""
20+
def count_components(self, n: int, edges: List[List[int]]) -> int:
21+
# ์—ฐ๊ฒฐ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ทธ๋ž˜ํ”„
22+
graph = [[] for _ in range(n)]
23+
24+
for a,b in edges:
25+
graph[a].append(b)
26+
graph[b].append(a)
27+
28+
# ๊นŠ์€ํƒ์ƒ‰์—์„œ ๋…ธ๋“œ ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ํ‘œ๊ธฐ
29+
visited = set()
30+
31+
def dfs(node):
32+
# ์žฌ๊ท€ํ•จ์ˆ˜ ์‹คํ–‰์‹œ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
33+
visited.add(node)
34+
for adj in graph[node]:
35+
if adj not in visited:
36+
dfs(adj)
37+
38+
count = 0
39+
for node in range(n):
40+
if node not in visited:
41+
dfs(node)
42+
count += 1
43+
return count
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution:
2+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
3+
4+
def remove_node(node):
5+
if node is None:
6+
return 0
7+
# 2. ์žฌ๊ท€ ํ˜ธ์ถœ
8+
count = remove_node(node.next) + 1
9+
# 4. ์‚ญ์ œ ๋กœ์ง (count๊ฐ€ n+1์ผ ๋•Œ)
10+
if count == n + 1:
11+
node.next = node.next.next
12+
# 5. ํ˜„์žฌ count ๋ฐ˜ํ™˜
13+
return count
14+
15+
# ์žฌ๊ท€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์ž‘
16+
total_count = remove_node(head)
17+
18+
if total_count == n:
19+
return head.next
20+
return head
21+
22+
23+
# Two Pointer Solution
24+
# One loop
25+
26+
class Solution:
27+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
28+
first = head
29+
# ์‚ญ์ œํ•  ๋…ธ๋“œ์˜ ๋ฐ”๋กœ ์ „ ๋…ธ๋“œ์— ๋„์ฐฉ
30+
for _ in range(n+1):
31+
first = first.next
32+
33+
dummy = ListNode(None, head)
34+
second = dummy
35+
36+
while first:
37+
first = first.next
38+
second = second.next
39+
40+
second.next = second.next.next
41+
42+
return dummy.next

โ€Žsame-tree/Seoya0512.pyโ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
9+
if p == None and q == None:
10+
return True
11+
12+
if p == None or q == None:
13+
return False
14+
15+
if p.val != q.val:
16+
return False
17+
18+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right,q.right)

0 commit comments

Comments
ย (0)