```python class Solution: def connectSticks(self, A: List[int]) -> int: “”” main idea: to minimize cost ==> to connect smallest stick pair as possible => PQ? tc O(NlgN) sc O(N) “”” n = len(A) if n <2: return 0 heapify(A) res = 0 while len(A)>1: x = heappop(A) y = heappop(A)...
[Read More]
1574. Shortest Subarray to be Removed to Make Array Sorted
Three round ```python class Solution: def findLengthOfShortestSubarray(self, arr: List[int]) -> int: “”” tc O(N) sc O(1) main idea: since we can remove only one subarray to make whole array sorted, we eigher remove (1) left sorted subarray’s right side (2) right sorted subarray’s left side (3) combined sorted left, right...
[Read More]
1052. Grumpy Bookstore Owner
Two round way ```python class Solution: def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int: “”” 1. get min happy customers by default, mark them to -1 2. sliding window left - right get the max additional customer tc O(N) sc O(1) “”” base = 0 for idx,(c,g) in...
[Read More]
645. Set Mismatch
Brute Force solution
"""
tc O(N) sc O(N)
"""
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
n = len(nums)
seen = set()
res = []
for x in nums:
if x in seen:
res.append(x)
else:
seen.add(x)
for i in range(1,n+1):
if i not in seen:
res.append(i)
return res
[Read More]
850. Rectangle Area II
```python
“””
tc O(N^2) sc O(N)
step1 :get unique x, sorted
step2:record idx of each x position, initialize cnt for tracking overlaps in at [x[i],x[i+1]]
step3: sort by y , use +1,-1 as mark to check if exist overlaps
area S = (cur_y - prev_y) * cur_x_sum
[Read More]