Skip to content

Commit 72fd840

Browse files
authored
Merge pull request #1050 from KwonNayeon/main
[KwonNayeon] Week 12
2 parents 36cb4c3 + a0a82eb commit 72fd840

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Constraints:
3+
- 1 <= intervals.length <= 10^5
4+
- intervals[i].length == 2
5+
- -5 * 10^4 <= starti < endi <= 5 * 10^4
6+
7+
Time Complexity: O(n * log n)
8+
- 구간 정렬 시 O(n * log n) 사용, 정렬된 구간을 순회할 때 O(n)
9+
10+
Space Complexity: O(1)
11+
- 정해진 변수 외에는 공간 사용하지 않음
12+
13+
풀이방법:
14+
1. 끝점을 기준으로 오름차순 정렬 <- 제거할 구간의 수를 최소화하기 위해
15+
2. 첫 번째 구간을 선택하고 그 구간의 끝점을 기록함
16+
3. 두 번째 구간부터 end보다 시작점이 크거나 같은(겹치지 않는) 구간을 찾아서 카운트
17+
4. 전체 구간의 수에서 count를 빼서 제거해야 할 구간의 수를 반환
18+
"""
19+
class Solution:
20+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
21+
if not intervals:
22+
return 0
23+
24+
intervals.sort(key=lambda x: x[1])
25+
26+
count = 1
27+
end = intervals[0][1]
28+
29+
for interval in intervals[1:]:
30+
31+
if interval[0] >= end:
32+
count += 1
33+
end = interval[1]
34+
35+
return len(intervals) - count
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Constraints:
3+
- The number of nodes in the list is sz.
4+
- 1 <= sz <= 30
5+
- 0 <= Node.val <= 100
6+
- 1 <= n <= sz
7+
8+
Time Complexity: O(n)
9+
- 여기서 n은 리스트의 길이, 리스트를 한 번만 순회함
10+
11+
Space Complexity: O(1)
12+
- 추가 공간을 사용하지 않음, 정해진 개수의 변수만 사용
13+
14+
풀이방법:
15+
1. fast 포인터를 n번 앞으로 이동
16+
2. base case: 만약 fast가 None이라면, 첫 번째 노드를 제거함
17+
3. fast.next가 None일 때까지 두 포인터를 함께 이동
18+
4. slow의 다음 노드를 제거함 (slow.next = slow.next.next로 연결을 끊어냄)
19+
"""
20+
# Definition for singly-linked list.
21+
# class ListNode:
22+
# def __init__(self, val=0, next=None):
23+
# self.val = val
24+
# self.next = next
25+
class Solution:
26+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
27+
slow = head
28+
fast = head
29+
30+
for i in range(n):
31+
fast = fast.next
32+
33+
if not fast:
34+
return head.next
35+
36+
while fast.next:
37+
slow = slow.next
38+
fast = fast.next
39+
40+
slow.next = slow.next.next
41+
42+
return head

same-tree/KwonNayeon.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Constraints:
3+
- The number of nodes in both trees is in the range [0, 100].
4+
- -10^4 <= Node.val <= 10^4
5+
6+
Time Complexity: O(n)
7+
- 각 노드를 한 번씩 방문
8+
9+
Space Complexity: O(h)
10+
- 재귀 호출 스택의 깊이는 트리의 높이(h)에 비례함
11+
12+
풀이방법:
13+
1. DFS와 재귀를 활용하여 두 트리를 동시에 탐색
14+
2. base case:
15+
- p와 q가 모두 None이면 → 같은 트리
16+
- 둘 중 하나만 None이거나 노드의 값이 다르면 → 다른 트리
17+
3. 재귀로 왼쪽과 오른쪽 서브트리가 모두 같은지 확인
18+
"""
19+
# Definition for a binary tree node.
20+
# class TreeNode:
21+
# def __init__(self, val=0, left=None, right=None):
22+
# self.val = val
23+
# self.left = left
24+
# self.right = right
25+
class Solution:
26+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
27+
if p is None and q is None:
28+
return True
29+
30+
if p is None or q is None or p.val != q.val:
31+
return False
32+
33+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
34+

0 commit comments

Comments
 (0)