LeetCode 题解工作台
统计移除递增子数组的数目 I
给你一个下标从 0 开始的 正 整数数组 nums 。 如果 nums 的一个子数组满足:移除这个子数组后剩余元素 严格递增 ,那么我们称这个子数组为 移除递增 子数组。比方说, [5, 3, 4, 6, 7] 中的 [3, 4] 是一个移除递增子数组,因为移除该子数组后, [5, 3, 4, 6,…
4
题型
5
代码语言
3
相关题
当前训练重点
简单 · 二分·搜索·答案·空间
答案摘要
根据题目描述,移除一个子数组后,剩余元素严格递增,那么存在以下几种情况: 1. 剩余元素仅包含数组 的前缀(可以为空);
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路
题目描述
给你一个下标从 0 开始的 正 整数数组 nums 。
如果 nums 的一个子数组满足:移除这个子数组后剩余元素 严格递增 ,那么我们称这个子数组为 移除递增 子数组。比方说,[5, 3, 4, 6, 7] 中的 [3, 4] 是一个移除递增子数组,因为移除该子数组后,[5, 3, 4, 6, 7] 变为 [5, 6, 7] ,是严格递增的。
请你返回 nums 中 移除递增 子数组的总数目。
注意 ,剩余元素为空的数组也视为是递增的。
子数组 指的是一个数组中一段非空且连续的元素序列。
示例 1:
输入:nums = [1,2,3,4] 输出:10 解释:10 个移除递增子数组分别为:[1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4] 和 [1,2,3,4]。移除任意一个子数组后,剩余元素都是递增的。注意,空数组不是移除递增子数组。
示例 2:
输入:nums = [6,5,7,8] 输出:7 解释:7 个移除递增子数组分别为:[5], [6], [5,7], [6,5], [5,7,8], [6,5,7] 和 [6,5,7,8] 。 nums 中只有这 7 个移除递增子数组。
示例 3:
输入:nums = [8,7,6,6] 输出:3 解释:3 个移除递增子数组分别为:[8,7,6], [7,6,6] 和 [8,7,6,6] 。注意 [8,7] 不是移除递增子数组因为移除 [8,7] 后 nums 变为 [6,6] ,它不是严格递增的。
提示:
1 <= nums.length <= 501 <= nums[i] <= 50
解题思路
方法一:双指针
根据题目描述,移除一个子数组后,剩余元素严格递增,那么存在以下几种情况:
- 剩余元素仅包含数组 的前缀(可以为空);
- 剩余元素仅包含数组 的后缀;
- 剩余元素包含数组 的前缀和后缀。
其中第 和第 种情况可以合并为一种情况,即剩余元素包含数组 的后缀。即一共有以下两种情况:
- 剩余元素仅包含数组 的前缀(可以为空);
- 剩余元素包含数组 的后缀。
我们先考虑第一种情况,即剩余元素仅包含数组 的前缀。我们可以用一个指针 指向数组 的最长递增前缀的最后一个元素,即 ,那么剩余元素的个数为 ,其中 为数组 的长度。因此,对于这种情况,要使得剩余元素严格递增,我们可以选择移除以下子数组:
- ;
- ;
- ;
- ;
- ;
- 。
这一共有 种情况,因此对于这种情况,移除递增子数组的数目为 。
再考虑第二种情况,即剩余元素包含数组 的后缀。我们可以用一个指针 指向数组 的递增后缀的第一个元素,我们在 的范围内枚举 作为递增后缀的第一个元素,每一次,我们需要移动指针 使得 ,那么移除递增子数组的数组增加 。当 时,我们停止枚举,因为此时后缀不是严格递增。
时间复杂度 ,其中 是数组 的长度。空间复杂度 。
class Solution:
def incremovableSubarrayCount(self, nums: List[int]) -> int:
i, n = 0, len(nums)
while i + 1 < n and nums[i] < nums[i + 1]:
i += 1
if i == n - 1:
return n * (n + 1) // 2
ans = i + 2
j = n - 1
while j:
while i >= 0 and nums[i] >= nums[j]:
i -= 1
ans += i + 2
if nums[j - 1] >= nums[j]:
break
j -= 1
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Does the candidate use binary search effectively to optimize the subarray check process?
- question_mark
Can the candidate explain the early termination technique when validating subarrays?
- question_mark
Is the candidate aware of how to optimize the solution for larger arrays by reducing unnecessary checks?
常见陷阱
外企场景- error
Failing to recognize the need for an efficient subarray check can lead to an O(n^3) solution.
- error
Not using binary search correctly, which could make the solution inefficient for larger inputs.
- error
Ignoring the early termination condition when checking subarrays, resulting in excessive computation time.
进阶变体
外企场景- arrow_right_alt
Extend the problem to handle arrays with non-positive integers.
- arrow_right_alt
Consider a version where removing multiple non-contiguous subarrays can also make the array strictly increasing.
- arrow_right_alt
Test the solution for larger input sizes, such as arrays with 100 elements, to measure performance.