""" main idea: use string == string2 comparison. since target string is fixed(needle) => length already know. all we need to do is to loop through string1, each index check start from current index to length of needle, if it's same as needle edge case: needle = '' ; optimization:...
[Read More]
LC41 First Missing Positive
class Solution: def firstMissingPositive(self, nums: List[int]) -> int: """ O(N)/O(1) in order to find first missing positive, need to make sure array sorted 1. loop through num, take num-1 as idx, place num to sorted order (not num is within [0,N]) 2. loop array, if nums[i]-1 != i, i+1 the...
[Read More]
LC897 Increasing Order Search Tree
Wrong solution(create a new tre is not allowded) but easy to understand : # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: # time O(N) sapace O(N) def increasingBST(self,...
[Read More]
LC713 Subarray Product Less Than K
```python “”” main idea: two pointer + sliding window iterate through whole array while increment count; until product reached to K, left pointer increment until meet criteria < K to continue next cnt note when incrementing left pointer, make sure l <= r, or else l will out of boundry...
[Read More]
LC399 Evaluate Division
BFS solution ```python “”” main idea: transform a/b=k1, b/c=k2 to a ===(cost:k1 )==> b ==(cost:k2)=> c for query a/c question, convert it into find the shortest cost from a to c. The cost is k1 * k2 step1: build a two dimensional graph in the form of adjacency list.Dividend is...
[Read More]