300_ Longest Increasing Subsequence
Input:[10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.Solution 1:
def lengthOfLIS(nums):
"""
:type nums: List[int]
:rtype: int
"""
output = [1] * len(nums)
for i in range(1, len(nums)):
output[i] = 1 + max([output[j] for j in range(i) if nums[j] < nums[i]] + [0])
# [0] is for nothing from nums[j] is less than nums[i]
# parameter inside max() can't be empty
return max(output + [0]) # [0] is for empty numsSolution 2:
Last updated