I am working on the following leetcode problem:
Given an integer array nums, return the length of the longest strictly increasing subsequence.
The solution given is O(N^2) time but I have the following solution that does not work in it's current state:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [-1 for i in range(len(nums))]
dp[0] = 1
for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
if i >= 2:
if dp[i] > dp[i-2]:
dp[i] = max(dp[i-1], dp[i-2], 0) + 1
else:
dp[i] = dp[i-1] + 1
else:
dp[i] = dp[i-1] + 1
else:
dp[i] = 1
return max(dp)
I am trying to get a O(N) solution (if it is possible but I am not sure!). My logic is as follows to choose dp[i] by either deleting an element if needed or picking it otherwise. Is it possible to fix my approach or should I bin it altoghether?