class Solution: def productExceptSelf(self, A: List[int]) -> List[int]: """ tc O(N) sc O(N) use two seperate array to cache current item in Array A's left range and right range 's accumutive product loop through the index get the combined range product res for current index L[i]: for A[i], its left...
[Read More]
LC378. Kth Smallest Element in a Sorted Matrix
Heap Solution ```python class Solution: def kthSmallest(self, matrix: List[List[int]], k: int) -> int: # time O(NlgN) space O(1) n = len(matrix) lo = matrix[0][0] hi = matrix[-1][-1] cnt,candi = 0,0 while lo < hi: mid = lo + (hi-lo)//2 cnt,candi = self.cnt_less_equal(matrix,mid) #print(cnt,candi,"aaaaaaaaaa") # before mid, # of valuse <...
[Read More]
LC209. Minimum Size Subarray Sum
```python class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: “”” sliding window, 2 loops 1. initialize left ptr, right ptr local sum 2. loop array, get accumutive local sum 3. when local sum >= s, move left ptr and compare with global min size (ans) 4. check if...
[Read More]
LC134. Gas Station
class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: """ time O(N) space O(1) 1. loop through gas, cost array, get total gas, total cost 2. during iteration, check current tank < 0, if yes, reset current tank, start from i + 1 3. check if total cost <=...
[Read More]
LC19. Remove Nth Node From End of List
```python time O(N) space O(N) class Solution: def numDecodings(self, s: str) -> int: n = len(s) dp = [0]*(n+1) # case 1 not 0 # case 2 0 # dp[i] += dp[i-1] || += dp[i-2] , dp[i]: there are dp[i] case to decode for string s[:i] dp[0] = 1 #...
[Read More]