Subject description:
A disorder of a given integer array, the length of the longest found rising sequence.
Example:
Input: [10,9,2,5,3,7,101,18]
Output: 4
explained: the longest sequence is increased [2,3,7,101], its length is 4.
Description:
Various combinations may be increased up sequence, you only need to output a corresponding length.
The time complexity of your algorithm should be O (n2).
Advanced: You can reduce the time complexity of the algorithm to O (n log n) do?
python code
class Solution:
# 动态规划的思路:将 dp 数组定义为:以 nums[i] 结尾的最长上升子序列的长度
# 那么题目要求的,就是这个 dp 数组中的最大者
# 以数组 [10, 9, 2, 5, 3, 7, 101, 18] 为例:
# dp 的值: 1 1 1 2 2 3 4 4
def lengthOfLIS(self, nums: List[int]) -> int:
size = len(nums)
if size <= 1:
return size
dp = [1] * size
for i in range(1, size):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)