跳至主要內容

300. Longest Increasing Subsequence


300. Longest Increasing Subsequenceopen in new window

🟠   🔖  数组 二分查找 动态规划  🔗 LeetCodeopen in new window

题目

Given an integer array nums, return the length of the longest strictly increasing _ subsequence_.

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^4 <= nums[i] <= 10^4

Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?

题目大意

给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。

子序列 是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。

进阶:

你能将算法的时间复杂度降低到 O(n logn) 吗?

解题思路

思路一:动态规划

可以使用动态规划来解决:

  • 创建一个长度为 n 的数组 dp,其中 dp[i] 表示以第 i 个元素为结尾的最长递增子序列的长度。
  • 初始化 dp 数组的所有元素为 1,因为每个元素自身也是一个长度为 1 的递增子序列。
  • 对于每个位置 i,遍历 0i-1 的所有位置,如果 nums[i] > nums[j],说明 nums[i] 可以接在 nums[j] 后面构成一个更长的递增子序列,更新 dp[i] = Math.max(dp[i], dp[j] + 1)
  • 最终,dp 数组中的最大值即为所求的最长递增子序列的长度。

复杂度分析

  • 时间复杂度: O(n^2) ,其中 n 是数组 nums 的长度。主要的时间复杂度来自于两层嵌套的循环,外层循环遍历数组中的每个元素,而内层循环在每次外层循环中都遍历了之前的所有元素。
  • 空间复杂度: O(n),使用了一个长度为 n 的数组来存储中间状态。

思路二:二分查找

使用二分查找优化最长递增子序列问题,主要利用了一个辅助数组 tails。这个数组在遍历过程中,始终保持递增的状态。辅助数组 tails 的长度 len 表示当前已经找到的最长递增子序列的长度。二分查找的过程如下:

  1. 初始化左右指针:

    • 初始时,left 指向 0,right 指向 len
  2. 开始二分查找:

    • 在当前的辅助数组 tails 中进行二分查找,找到第一个大于等于 nums[i] 的位置。用 mid 表示二分查找中间位置。
    • 如果 tails[mid] < nums[i],说明当前的递增子序列可以继续延长,因此更新 left = mid + 1
    • 否则,说明当前递增子序列需要进行调整,因此更新 right = mid
  3. 更新辅助数组:

    • 如果 left === len,说明 nums[i] 大于当前递增子序列的所有元素,将 nums[i] 添加到辅助数组的末尾,并且递增子序列的长度 len++
    • 否则,将 nums[i] 替换掉辅助数组中第一个大于等于 nums[i] 的元素。
  4. 迭代下一个元素:

    • 重复上述过程,直到遍历完整个数组 nums
  5. 最终结果:

    • 辅助数组的长度 len 即为最长递增子序列的长度。

举一个具体的示例来说明:

假设 nums = [10, 9, 2, 5, 3, 7, 101, 18]

  • 初始化时,辅助数组 tails 为空,len = 0
  • 当处理元素 nums[0] = 10 时,tails 为空,将 10 加入到 tailslen 变为 1
  • 当处理元素 nums[1] = 9 时,通过二分查找在 tails 中找到第一个大于等于 9 的位置,将 tails[0] 替换为 9
  • 依此类推,处理完整个数组后,tails[2, 3, 7, 18]len = 4,最终结果为 4

这种方法的核心在于通过二分查找,高效地维护了一个递增的辅助数组,从而在保证正确性的同时降低时间复杂度到 O(n logn)

复杂度分析

  • 时间复杂度: O(n logn) ,其中 n 是数组 nums 的长度。
  • 空间复杂度: O(len),其中 len 是最长递增子序列的长度,使用了一个长度最长为 len 的辅助数组。

代码

动态规划
/**
 * @param {number[]} nums
 * @return {number}
 */
var lengthOfLIS = function (nums) {
	let dp = new Array(nums.length).fill(1);
	for (let i = 0; i < nums.length; i++) {
		for (let j = 0; j < i; j++) {
			if (nums[i] > nums[j]) {
				dp[i] = Math.max(dp[i], dp[j] + 1);
			}
		}
	}

	return Math.max(...dp);
};

相关题目

相关题目
- [334. 递增的三元子序列](https://leetcode.com/problems/increasing-triplet-subsequence)
- [354. 俄罗斯套娃信封问题](./0354.md)
- [646. 最长数对链](https://leetcode.com/problems/maximum-length-of-pair-chain)
- [673. 最长递增子序列的个数](https://leetcode.com/problems/number-of-longest-increasing-subsequence)
- [712. 两个字符串的最小 ASCII 删除和](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings)
- [1671. 得到山形数组的最少删除次数](https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array)
- [1964. 找出到每个位置为止最长的有效障碍赛跑路线](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position)
- [2111. 使数组 K 递增的最少操作次数](https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing)
- [2370. 最长理想子序列](https://leetcode.com/problems/longest-ideal-subsequence)
- [🔒 Maximum Number of Books You Can Take](https://leetcode.com/problems/maximum-number-of-books-you-can-take)
- [2407. 最长递增子序列 II](https://leetcode.com/problems/longest-increasing-subsequence-ii)