DP, Binary Search, and Monotonic queue

DP solution

# time complexity: O(n^2)# Space complexity: O(n)
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1]*len(nums)
for i in range(1, len(nums)):
for j in range(i):
if nums[i]>nums[j]:
dp[i] = max(dp[j]+1, dp[i])
return max(dp) if dp else 0

Reference

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store