TreeMap(Binary Tree python) main idea: create a binary tree where each node store start and end time ,and meanwhile can track left and right neighbour. Note, left neighbour means its end time is less than current node’s start time, vise versa. ```python time O(NlgN), worst N^2 space O(N) class Node:...
[Read More]
849. Maximize Distance to Closest Person
Two Pointer: main idea- loop through each seat and check if there is a person. record previous occupied seat index after move to next position. find the max distance between current seat with pp and previous seat with people. (cur - prev ) // 2 will be idea seat. keep...
[Read More]
129. Sum Root to Leaf Numbers
```python 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: time O(N) space O(H) def sumNumbers(self, root: TreeNode) -> int: self._sum = 0 def preOrder(node,cur): if not node: return cur = 10 * cur +node.val...
[Read More]
228. Summary Ranges
```python
“””
main idea: keep check if start and end point is the same. if not using format with ->, else push start point only, restart a new start for the next loop.
[Read More]
547. Friend Circles
DFS solution # time O(N^2) space O(N) class Solution: def findCircleNum(self, M: List[List[int]]) -> int: size = len(M) cnt = 0 visited = [False] * size for i in range(size): if(not visited[i]): self.dfs(M,visited,i) cnt += 1 return cnt def dfs(self,M,visited, person): for other in range(len(M)): # found another person in...
[Read More]