LeetCode 题解工作台

统计移除递增子数组的数目 II

给你一个下标从 0 开始的 正 整数数组 nums 。 如果 nums 的一个子数组满足:移除这个子数组后剩余元素 严格递增 ,那么我们称这个子数组为 移除递增 子数组。比方说, [5, 3, 4, 6, 7] 中的 [3, 4] 是一个移除递增子数组,因为移除该子数组后, [5, 3, 4, 6,…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

困难 · 二分·搜索·答案·空间

bolt

答案摘要

根据题目描述,移除一个子数组后,剩余元素严格递增,那么存在以下几种情况: 1. 剩余元素仅包含数组 的前缀(可以为空);

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 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 <= 105
  • 1 <= nums[i] <= 109
lightbulb

解题思路

方法一:双指针

根据题目描述,移除一个子数组后,剩余元素严格递增,那么存在以下几种情况:

  1. 剩余元素仅包含数组 numsnums 的前缀(可以为空);
  2. 剩余元素仅包含数组 numsnums 的后缀;
  3. 剩余元素包含数组 numsnums 的前缀和后缀。

其中第 22 和第 33 种情况可以合并为一种情况,即剩余元素包含数组 numsnums 的后缀。即一共有以下两种情况:

  1. 剩余元素仅包含数组 numsnums 的前缀(可以为空);
  2. 剩余元素包含数组 numsnums 的后缀。

我们先考虑第一种情况,即剩余元素仅包含数组 numsnums 的前缀。我们可以用一个指针 ii 指向数组 numsnums 的最长递增前缀的最后一个元素,即 nums[0]<nums[1]<<nums[i]nums[0] \lt nums[1] \lt \cdots \lt nums[i],那么剩余元素的个数为 ni1n - i - 1,其中 nn 为数组 numsnums 的长度。因此,对于这种情况,要使得剩余元素严格递增,我们可以选择移除以下子数组:

  1. nums[i+1,...,n1]nums[i+1,...,n-1]
  2. nums[i,...,n1]nums[i,...,n-1]
  3. nums[i1,...,n1]nums[i-1,...,n-1]
  4. nums[i2,...,n1]nums[i-2,...,n-1]
  5. \cdots
  6. nums[0,...,n1]nums[0,...,n-1]

这一共有 i+2i + 2 种情况,因此对于这种情况,移除递增子数组的数目为 i+2i + 2

再考虑第二种情况,即剩余元素包含数组 numsnums 的后缀。我们可以用一个指针 jj 指向数组 numsnums 的递增后缀的第一个元素,我们在 [n1,...,1][n - 1,...,1] 的范围内枚举 jj 作为递增后缀的第一个元素,每一次,我们需要移动指针 ii 使得 nums[i]<nums[j]nums[i] \lt nums[j],那么移除递增子数组的数组增加 i+2i + 2。当 nums[j1]nums[j]nums[j - 1] \ge nums[j] 时,我们停止枚举,因为此时后缀不是严格递增。

时间复杂度 O(n)O(n),其中 nn 是数组 numsnums 的长度。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The candidate understands how to optimize subarray searches using binary search and pointers.

  • question_mark

    The candidate effectively uses prefix arrays or similar techniques to track increasing sequences.

  • question_mark

    The candidate can break down a problem requiring multiple steps into a clear, efficient approach.

warning

常见陷阱

外企场景
  • error

    Misunderstanding the problem's constraints, leading to an inefficient solution.

  • error

    Failing to optimize the subarray search, resulting in excessive time complexity.

  • error

    Overlooking edge cases where subarrays are close to the entire array, requiring careful handling of boundaries.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing the size of the input array to test scalability.

  • arrow_right_alt

    Handling arrays with larger values (e.g., elements in the range of 10^9).

  • arrow_right_alt

    Exploring alternate approaches like dynamic programming or greedy algorithms for subarray validation.

help

常见问题

外企场景

统计移除递增子数组的数目 II题解:二分·搜索·答案·空间 | LeetCode #2972 困难