diff --git a/non-overlapping-intervals/yuseok89.py b/non-overlapping-intervals/yuseok89.py new file mode 100644 index 0000000000..cc08350456 --- /dev/null +++ b/non-overlapping-intervals/yuseok89.py @@ -0,0 +1,12 @@ +# TC: O(NlogN) +# SC: O(1) +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + end, cnt = float('-inf'), 0 + for s, e in sorted(intervals, key=lambda x: x[1]): + if s >= end: + end = e + else: + cnt += 1 + return cnt + diff --git a/remove-nth-node-from-end-of-list/yuseok89.py b/remove-nth-node-from-end-of-list/yuseok89.py new file mode 100644 index 0000000000..21d8ff2574 --- /dev/null +++ b/remove-nth-node-from-end-of-list/yuseok89.py @@ -0,0 +1,26 @@ +# TC: O(N) +# SC: O(1) +# 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]: + + fwd, flw = head, head + + for _ in range(n): + fwd = fwd.next + + if not fwd: + return head.next + + while fwd.next: + fwd = fwd.next + flw = flw.next + + flw.next = flw.next.next + + return head + diff --git a/same-tree/yuseok89.py b/same-tree/yuseok89.py new file mode 100644 index 0000000000..f00f2b2ea8 --- /dev/null +++ b/same-tree/yuseok89.py @@ -0,0 +1,20 @@ +# TC: O(N) +# SC: O(H) +# 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 and q: + if p.val != q.val: + return False + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) + elif not p and not q: + return True + else: + return False + diff --git a/serialize-and-deserialize-binary-tree/yuseok89.py b/serialize-and-deserialize-binary-tree/yuseok89.py new file mode 100644 index 0000000000..4fd3f06047 --- /dev/null +++ b/serialize-and-deserialize-binary-tree/yuseok89.py @@ -0,0 +1,74 @@ +# TC: O(N) +# SC: O(N) +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Codec: + + def serialize(self, root): + """Encodes a tree to a single string. + + :type root: TreeNode + :rtype: str + """ + + arr = [] + q = deque() + q.append(root) + + while q: + + cur = q.popleft() + + if cur: + arr.append(str(cur.val)) + q.append(cur.left) + q.append(cur.right) + else: + arr.append('n') + + return ','.join(arr) + + + def deserialize(self, data): + """Decodes your encoded data to tree. + + :type data: str + :rtype: TreeNode + """ + + values = data.split(',') + n = len(values) + + if values[0] == 'n': + return None + + root = TreeNode(int(values[0])) + q = deque() + q.append(root) + idx = 1 + + while idx < n: + par = q.popleft() + + if values[idx] != 'n': + par.left = TreeNode(int(values[idx])) + q.append(par.left) + idx += 1 + + if idx < n and values[idx] != 'n': + par.right = TreeNode(int(values[idx])) + q.append(par.right) + idx += 1 + + return root + +# Your Codec object will be instantiated and called as such: +# ser = Codec() +# deser = Codec() +# ans = deser.deserialize(ser.serialize(root)) +