Main idea: current_presum - old_presum = target , where old_presum is in hmap, so we can check if current_presum - target in hmap. if yes, res + 1; then update current_presum in hmap before entereing subtree. Note, each time when current node and its subtrees are traversed, hmap need to...
[Read More]
LC15 3Sum
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: # main idea: sort + two pointers # time O(N^2) space O(1) nums.sort() res = [] size = len(nums) for i in range(size-2): # optimization: since nums is sorted, it is impossible to have a sum of 0 if nums[i] > 0...
[Read More]
LC987 Vertical Order Traversal of a Binary Tree
link
[Read More]
LC202 Happy Number
link
[Read More]
LC556 Next Greater Element III
class Solution: def nextGreaterElement(self, n: int) -> int: # time O(NlgN) worst 2N, space O(N), where N is # digits ? # notice, 32bit max interger is 2^31 -1 # step1, reverse digits, find target number with, idx smaller than lower digit # step2, reverve its lower digit then loop...
[Read More]