最长上升子序列:
from bisect import bisect_left
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = []
for i in nums:
index = bisect_left(result,i)
if index == len(result):
result.append(i)
else:
result[index] = i
return len(result)
该方法时间复杂度为nlogn,动态规划的时间复杂度为n2。
最长连续递增子序列:
class Solution(object):
def findLengthOfLCIS(self, nums):
result = 0
start = 0
for i in range(len(nums)):
if i and nums[i-1] >= nums[i]:
start = i
result = max(result, i - start + 1)
return result
时间复杂度O(n).