diff --git a/non-overlapping-intervals/KwonNayeon.py b/non-overlapping-intervals/KwonNayeon.py new file mode 100644 index 000000000..a6d83fd79 --- /dev/null +++ b/non-overlapping-intervals/KwonNayeon.py @@ -0,0 +1,36 @@ +""" +Constraints: +- 1 <= intervals.length <= 10^5 +- intervals[i].length == 2 +- -5 * 10^4 <= starti < endi <= 5 * 10^4 + +Time Complexity: O(n * log n) +- 구간 정렬 시 O(n * log n) 사용, 정렬된 구간을 순회할 때 O(n) + +Space Complexity: O(1) +- 정해진 변수 외에는 공간 사용하지 않음 + +풀이방법: +1. 끝점을 기준으로 오름차순 정렬 <- 제거할 구간의 수를 최소화하기 위해 +2. 첫 번째 구간을 선택하고 그 구간의 끝점을 기록함 +3. 두 번째 구간부터 end보다 시작점이 크거나 같은(겹치지 않는) 구간을 찾아서 카운트 +4. 전체 구간의 수에서 count를 빼서 제거해야 할 구간의 수를 반환 +""" +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + if not intervals: + return 0 + + intervals.sort(key=lambda x: x[1]) + + count = 1 + end = intervals[0][1] + + for interval in intervals[1:]: + + if interval[0] >= end: + count += 1 + end = interval[1] + + return len(intervals) - count + diff --git a/remove-nth-node-from-end-of-list/KwonNayeon.py b/remove-nth-node-from-end-of-list/KwonNayeon.py new file mode 100644 index 000000000..24914e107 --- /dev/null +++ b/remove-nth-node-from-end-of-list/KwonNayeon.py @@ -0,0 +1,42 @@ +""" +Constraints: +- The number of nodes in the list is sz. +- 1 <= sz <= 30 +- 0 <= Node.val <= 100 +- 1 <= n <= sz + +Time Complexity: O(n) +- 여기서 n은 리스트의 길이, 리스트를 한 번만 순회함 + +Space Complexity: O(1) +- 추가 공간을 사용하지 않음, 정해진 개수의 변수만 사용 + +풀이방법: +1. fast 포인터를 n번 앞으로 이동 +2. base case: 만약 fast가 None이라면, 첫 번째 노드를 제거함 +3. fast.next가 None일 때까지 두 포인터를 함께 이동 +4. slow의 다음 노드를 제거함 (slow.next = slow.next.next로 연결을 끊어냄) +""" +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + slow = head + fast = head + + for i in range(n): + fast = fast.next + + if not fast: + return head.next + + while fast.next: + slow = slow.next + fast = fast.next + + slow.next = slow.next.next + + return head diff --git a/same-tree/KwonNayeon.py b/same-tree/KwonNayeon.py new file mode 100644 index 000000000..c7c7adee7 --- /dev/null +++ b/same-tree/KwonNayeon.py @@ -0,0 +1,34 @@ +""" +Constraints: +- The number of nodes in both trees is in the range [0, 100]. +- -10^4 <= Node.val <= 10^4 + +Time Complexity: O(n) +- 각 노드를 한 번씩 방문 + +Space Complexity: O(h) +- 재귀 호출 스택의 깊이는 트리의 높이(h)에 비례함 + +풀이방법: +1. DFS와 재귀를 활용하여 두 트리를 동시에 탐색 +2. base case: + - p와 q가 모두 None이면 → 같은 트리 + - 둘 중 하나만 None이거나 노드의 값이 다르면 → 다른 트리 +3. 재귀로 왼쪽과 오른쪽 서브트리가 모두 같은지 확인 +""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if p is None and q is None: + return True + + if p is None or q is None or p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) +