iterative solution """ main idea: By utilizing there is only + - in the expression. we can use stack to track sign in front of a parenthesis. each time a nom-digit number appear, res will need to be added with num. Note the sign is affected by its previous operator(+,-)...
[Read More]
LC84. Largest Rectangle in Histogram
```python “”” main idea: 1. keep a mono increasing stack, 2. until current height is shorter than ones on top of stack, keep setting top stack’s height as max height, difference between current idx and next stack top as width, caculate current area. 3.keep popping off lower height as target...
[Read More]
LC1282 Group the People Given the Group Size They Belong To
BFS solution is easier to understand ```python class Solution: def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool: # time O(MN) space O(MN) # step1, dir cache; track visited ;q; initialize start # step2: iterate q, termination condition: element on top q == destination # step3: current pos, go...
[Read More]
LC264. Ugly Number II
```python class Solution: def nthUglyNumber(self, n: int) -> int: # time O(1) space O(1) res = [1] i2,i3,i5= 0,0,0 while n > 1: u2,u3,u5 = res[i2]2,res[i3]3,res[i5]*5 minui = min(u2,u3,u5) if minui % 2 == 0: i2 += 1 if minui % 3 == 0: i3 += 1 if minui %...
[Read More]
LC263. Ugly Number
```python class Solution: def isUgly(self, num: int) -> bool: # step1: edge case - num <= 0 if num <= 0: return False # step2: strip num with all candidate prime factors until num reach mininum while num % 2 == 0: num = num//2 while num % 3 ==...
[Read More]