diff --git a/non-overlapping-intervals/okyungjin.py b/non-overlapping-intervals/okyungjin.py new file mode 100644 index 0000000000..5fc005fb25 --- /dev/null +++ b/non-overlapping-intervals/okyungjin.py @@ -0,0 +1,22 @@ +""" +https://leetcode.com/problems/non-overlapping-intervals/ + +Time: O(N log N), intervals 정렬 +Space: O(1) +""" +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + # end 오름차순 intervals 정렬 + intervals.sort(key=lambda x: x[1]) + + # remove count + count = 0 + last_end = float('-inf') + + for start, end in intervals: + if start >= last_end: # 구간 안 겹침 + last_end = end + else: # 구간 겹침 + count += 1 + + return count diff --git a/same-tree/okyungjin.py b/same-tree/okyungjin.py new file mode 100644 index 0000000000..bb59f630a3 --- /dev/null +++ b/same-tree/okyungjin.py @@ -0,0 +1,35 @@ +""" +https://leetcode.com/problems/same-tree/description/ + +N: min(p노드수, q노드수) +Time: O(N) +Space: O(N) +""" + +# 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: + queue = deque([(p, q)]) + + while queue: + node_p, node_q = queue.popleft() + + if not node_p and not node_q: + continue + + elif node_p and node_q: + if node_p.val == node_q.val: + queue.append((node_p.left, node_q.left)) + queue.append((node_p.right, node_q.right)) + else: + return False + + else: + return False + + return True