Lc1008

# 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 bstFromPreorder(self, A: List[int]) -> TreeNode: # O(N^2) # termination case: current sub arr is empty if not A: return... [Read More]

LC986. Interval List Intersections

class Solution: def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]: """ tc O(A+B) sc O(1) main idea: use two pointer to track overlapping area between two intervels. once one intervel exhaust, move to next one assuming both A,B list have overlaps 1. check corner case: one of them empty 2.... [Read More]
Tags: Two Pointers

LC977. Squares of a Sorted Array

class Solution: def sortedSquares(self, A: List[int]) -> List[int]: """ tc O(N) sc O(N) main idea: two ptr, since A is sorted, we can compare from two end, create a result arr to keep appending relative bigger squared value into res note while loop termination condition: l <= r """ n... [Read More]

LC975. Odd Even Jump

Stack solution class Solution: def oddEvenJumps(self, A): """ tc O(NlgN) sc O(N) """ n = len(A) next_higher_idx, next_lower_idx = [0] * n, [0] * n # step1 cache start odd jump, next position st = [] for a,i in sorted([a,i] for i, a in enumerate(A)): while st and st[-1] <... [Read More]

LC814. Binary Tree Pruning

# 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 pruneTree(self, root: TreeNode) -> TreeNode: """ tc O(N) sc O(h) h ~ N main idea: post order, if left/... [Read More]
Tags: Tree