diff --git a/binary-tree-maximum-path-sum/yuseok89.py b/binary-tree-maximum-path-sum/yuseok89.py new file mode 100644 index 0000000000..6bbf164ae8 --- /dev/null +++ b/binary-tree-maximum-path-sum/yuseok89.py @@ -0,0 +1,30 @@ +# 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 maxPathSum(self, root: Optional[TreeNode]) -> int: + + ans = float('-inf') + + def get_max(cur: Optional[TreeNode]) -> int: + nonlocal ans + + if cur is None: + return 0 + + left_max = get_max(cur.left) + right_max = get_max(cur.right) + + ans = max(ans, cur.val, cur.val + left_max, cur.val + right_max, cur.val +left_max + right_max) + + return max(cur.val, cur.val + max(left_max, right_max)) + + get_max(root) + + return ans + diff --git a/merge-intervals/yuseok89.py b/merge-intervals/yuseok89.py new file mode 100644 index 0000000000..8bf9d37398 --- /dev/null +++ b/merge-intervals/yuseok89.py @@ -0,0 +1,25 @@ +# TC: O(NlogN) +# SC: O(N) +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + + ans = list() + + intervals.sort(key=lambda x:x[0]) + + ans.append([intervals[0][0]]) + + end = intervals[0][1] + + for idx in range(1, len(intervals)): + if intervals[idx][0] <= end: + end = max(end, intervals[idx][1]) + else: + ans[-1].append(end) + ans.append([intervals[idx][0]]) + end = intervals[idx][1] + + ans[-1].append(end) + + return ans + diff --git a/missing-number/yuseok89.py b/missing-number/yuseok89.py new file mode 100644 index 0000000000..723c6dde27 --- /dev/null +++ b/missing-number/yuseok89.py @@ -0,0 +1,12 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def missingNumber(self, nums: List[int]) -> int: + n = len(nums) + total = n * (n + 1) // 2 + + for num in nums: + total -= num + + return total + diff --git a/reorder-list/yuseok89.py b/reorder-list/yuseok89.py new file mode 100644 index 0000000000..eea0f18281 --- /dev/null +++ b/reorder-list/yuseok89.py @@ -0,0 +1,37 @@ +# TC: O(N) +# SC: O(N) +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution: + def reorderList(self, head: Optional[ListNode]) -> None: + """ + Do not return anything, modify head in-place instead. + """ + + if not head or not head.next: + return + + def rec(right: Optional[ListNode]) -> Optional[ListNode]: + if not right: + return head + + left = rec(right.next) + + if not left: + return None + + if left == right or left.next == right: + right.next = None + return None + + nxt_left = left.next + left.next = right + right.next = nxt_left + + return nxt_left + + rec(head) +