DP class Solution: def lengthOfLIS(self, nums: List[int]) -> int: # dp[i]: longest subsequence length ending with nums[i] # time O(N^2) space O(N) # base case dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[j] < nums[i]: dp[i] = max(dp[i],dp[j] + 1) # get biggest...
[Read More]
62. Unique Paths
DP solution
[Read More]
563. Binary Tree Tilt
```python
[Read More]
503. Next Greater Element II
```python class Solution: def nextGreaterElements(self, nums: List[int]) -> List[int]: # main idea: double nums by using mode # time O(N) space O(N) # step1: create stack, initialize res st = [] size = len(nums) res = [-1] * size # step2: make stack strickly decreasing for i in range(2*size-1,-1,-1): #...
[Read More]
496. Next Greater Element I
```python class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: # time O(N+M), M = len(nums1), N = len(nums2) space O(N) # step1: create a stack, a hashmap st = [] d = {} # step2: store number in decreasing order for num in nums2: # step2.1 pop the...
[Read More]