135. Candy

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]
Tags: Greedy

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]
Tags: Array

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]
Tags: Design DLList