Given an integer array
nums
, return the length of the longest strictly increasing subsequence.A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example,
[3,6,2,7]
is a subsequence of the array [0,3,1,6,2,2,7]
.Example 1:
Input: nums = [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.
Example 2:
Input: nums = [0,1,0,3,2,3]
Output: 4
Example 3:
Input: nums = [7,7,7,7,7,7,7]
Output: 1
Constraints:
1 <= nums.length <= 2500
-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup>
Follow up: Can you come up with an algorithm that runs in
O(n log(n))
time complexity?DP算法思路:
N/A
注意事项:
- 初始化从1开始,因为一个数是递增序列
- 返回值不是dp[-1],而是所有dp的最大值
Python代码:
1 | [3, 5, 1, 3, 9, 5, 6] 1, 3, 5, 6 |
算法分析:
时间复杂度为O(n2)
,空间复杂度O(n2)
.
打印路径算法思路:
N/A
注意事项:
- path数组大小和原数组一样,因为path数组的下标和值都是下标,这样不用-1,直接nums[pos],不容易错
- 多了Line 11 - 12和14 - 15,需要记录产生最大值的下标
- 打印的时候path数组的下标和值都是下标,而值是前一个下标,所以是pos = positions[pos],循环次数为dp值,开始下标为dp值对应的下标path[:biggest_pos + 1]。
Python代码:
1 | def lengthOfLIS_with_path(self, nums: List[int]) -> int: |
bisect算法II解题思路:
N/A
注意事项:
- bisect_left类似于greater_or_equal_position但若equal时,取第一个,但greater_or_equal_position是取最后一个,此题没关系,因为相等的数会被替换掉,递增序列中不会存在相等的数
Python代码:
1 | def lengthOfLIS(self, nums: List[int]) -> int: |
算法分析:
时间复杂度为O(nlogn)
,空间复杂度O(n)
.