Two Array Solution class Solution: def candy(self, ratings: List[int]) -> int: """ main idea: two arrays to cache one side variation seperately and get the upper bound tc O(N) sc O(N) """ n = len(ratings) left = [1] * n right = [1]* n for i in range(1,n): if ratings[i]...
[Read More]
1779. Find Nearest Point That Has the Same X or Y Coordinate
Brute Froce class Solution: def addBinary(self, a: str, b: str) -> str: """ 1. find min len to loop 2. loop from low bit to high bit and add bits with carry 3. for remaining bit in # tc O(N) sc O(N) """ if len(a) <= len(b): n = len(a)...
[Read More]
1779. Find Nearest Point That Has the Same X or Y Coordinate
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: """ 1. find valid points (share same x or y ) 2. within valid points, smallest distance 3. res to record smallest index when first appear tc O(N) sc O(1) """ min_d = float('inf') res = -1 for...
[Read More]
432. All O`one Data Structure
```python
class Bucket:
def init(self,cnt = 0):
self.cnt = cnt
self.keySet = set()
self.pre = None
self.nxt = None
def _get_any_key(self):
if len(self.keySet)!=0:
res = self.keySet.pop()
self.keySet.add(res)
return res
else:
return None
max is on the tail while min is on the head
class AllOne:
[Read More]
1381. Design a Stack With Increment Operation
Brute force
```python
class CustomStack:
[Read More]