Solution 1: Sweepline ```python class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]: “”” tc O((M+N)lg(M+N)) sc O(M+N) main idea: use one list to store all time slots, tracking accumulating cnt and cache previous start time 1. append both slots’ start and end time into list with...
[Read More]
1271. Hexspeak
class Solution: def toHexspeak(self, num: str) -> str: """ 1. convert num -> hex 2. map hex digit with O~A 3. get rem of each mod 16, cache rem into arr 4. convert tc O(lgN) N: value of num sc O(lgN) """ d = {1:'I',0:'O',10:"A",11: "B",12: "C", 13:"D", 14:"E", 15:"F"}...
[Read More]
336. Palindrome Pairs
refer ```python class TrieNode: def init(self): self.children = {} self.idx = -1 self.res_palin_idx = [] class Solution: def palindromePairs(self, words: List[str]) -> List[List[int]]: “”” tc O(NWW) sc O(NW) W: max len of a word 1. create trieNode 2. add reversed words into trie 2.1 if at current trie node, current...
[Read More]
1740. Find Distance in a Binary Tree
```python class Solution: def findDistance(self, root: TreeNode, p: int, q: int) -> int: “”” tc O(N) sc O(H) “”” ### LCA of p, q def dfs(node): if not node or node.val in (q,p): return node left = dfs(node.left) right = dfs(node.right) if left and right : return node else: return...
[Read More]
LC815. Bus Routes
```python class Solution: def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int: “”” tc O(stops) sc O(stops) BFS on Bus(route), not on stops 1. map stops to route 2. BFS: take a stop and find all connected route, start from source stop, at its route, check unvisited route’s each...
[Read More]