LeetCode 题解工作台
最长递增子序列的个数
给定一个未排序的整数数组 nums , 返回最长递增子序列的个数 。 注意 这个数列必须是 严格 递增的。 示例 1: 输入: [1,3,5,4,7] 输出: 2 解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。 示例 2: 输入: [2,2,2,2,2]…
4
题型
6
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
我们定义 表示以 结尾的最长递增子序列的长度,定义 表示以 结尾的最长递增子序列的个数。初始时 , 。另外,定义 表示最长递增子序列的长度,定义 表示最长递增子序列的个数。 对于每一个 ,我们枚举 中的所有元素 ,如果 $nums[j] \lt nums[i]$,则 可以接在 后面,形成一个更长的递增子序列。如果 $f[i] \lt f[j] + 1$,说明以 结尾的最长递增子…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给定一个未排序的整数数组 nums , 返回最长递增子序列的个数 。
注意 这个数列必须是 严格 递增的。
示例 1:
输入: [1,3,5,4,7] 输出: 2 解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
示例 2:
输入: [2,2,2,2,2] 输出: 5 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
提示:
1 <= nums.length <= 2000-106 <= nums[i] <= 106
解题思路
方法一:动态规划
我们定义 表示以 结尾的最长递增子序列的长度,定义 表示以 结尾的最长递增子序列的个数。初始时 , 。另外,定义 表示最长递增子序列的长度,定义 表示最长递增子序列的个数。
对于每一个 ,我们枚举 中的所有元素 ,如果 ,则 可以接在 后面,形成一个更长的递增子序列。如果 ,说明以 结尾的最长递增子序列的长度增加了,那么我们需要更新 ,并且 。如果 ,说明我们找到了一条长度与之前相同的最长递增子序列,那么我们需要将 增加 。然后,如果 ,说明最长递增子序列的长度增加了,那么我们需要更新 ,并且 。如果 ,说明我们找到了一条长度与之前相同的最长递增子序列,那么我们需要将 增加 。
最后,我们返回 即可。
时间复杂度 ,空间复杂度 。其中 是数组 的长度。
class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
n = len(nums)
f = [1] * n
cnt = [1] * n
mx = 0
for i in range(n):
for j in range(i):
if nums[j] < nums[i]:
if f[i] < f[j] + 1:
f[i] = f[j] + 1
cnt[i] = cnt[j]
elif f[i] == f[j] + 1:
cnt[i] += cnt[j]
if mx < f[i]:
mx = f[i]
ans = cnt[i]
elif mx == f[i]:
ans += cnt[i]
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(n^2) |
| 空间 | O(n) |
面试官常问的追问
外企场景- question_mark
Candidate should be comfortable explaining the state transition dynamic programming approach.
- question_mark
Look for understanding of optimization techniques, such as reducing space complexity or utilizing a BIT.
- question_mark
Assess the candidate's ability to handle larger inputs efficiently and discuss the trade-offs between time and space complexity.
常见陷阱
外企场景- error
Forgetting to update both dp and count arrays correctly during the iterations can lead to wrong counts.
- error
Misunderstanding the problem constraints can lead to inefficient solutions that fail for larger inputs.
- error
Not recognizing the need for optimization, especially in terms of space complexity when handling large datasets.
进阶变体
外企场景- arrow_right_alt
Find the number of longest increasing subsequences with additional constraints on elements.
- arrow_right_alt
Consider variations that involve non-strictly increasing subsequences or variations in element restrictions.
- arrow_right_alt
Handle cases with negative numbers and zeroes in the array, ensuring that these do not affect the LIS calculation.