LeetCode 题解工作台

分割数组为连续子序列

给你一个按 非递减顺序 排列的整数数组 nums 。 请你判断是否能在将 nums 分割成 一个或多个子序列 的同时满足下述 两个 条件: 每个子序列都是一个 连续递增序列 (即,每个整数 恰好 比前一个整数大 1 )。 所有子序列的长度 至少 为 3 。 如果可以分割 nums 并满足上述条件,则…

category

4

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

由于题目中的子序列是由连续整数组成的,因此,只要知道子序列的最后一个数以及子序列的长度,就能够确定子序列。 我们可以使用哈希表存储每个子序列的最后一个数,使用优先队列存储当前数作为子序列的末尾时,子序列的长度。我们要优先选择长度较短的子序列,因此使用小根堆。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个按 非递减顺序 排列的整数数组 nums

请你判断是否能在将 nums 分割成 一个或多个子序列 的同时满足下述 两个 条件:

  • 每个子序列都是一个 连续递增序列(即,每个整数 恰好 比前一个整数大 1 )。
  • 所有子序列的长度 至少3

如果可以分割 nums 并满足上述条件,则返回 true ;否则,返回 false

 

示例 1:

输入:nums = [1,2,3,3,4,5]
输出:true
解释:nums 可以分割成以下子序列:
[1,2,3,3,4,5] --> 1, 2, 3
[1,2,3,3,4,5] --> 3, 4, 5

示例 2:

输入:nums = [1,2,3,3,4,4,5,5]
输出:true
解释:nums 可以分割成以下子序列:
[1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
[1,2,3,3,4,4,5,5] --> 3, 4, 5

示例 3:

输入:nums = [1,2,3,4,4,5]
输出:false
解释:无法将 nums 分割成长度至少为 3 的连续递增子序列。

 

提示:

  • 1 <= nums.length <= 104
  • -1000 <= nums[i] <= 1000
  • nums 按非递减顺序排列
lightbulb

解题思路

方法一:哈希表 + 优先队列(小根堆)

由于题目中的子序列是由连续整数组成的,因此,只要知道子序列的最后一个数以及子序列的长度,就能够确定子序列。

我们可以使用哈希表存储每个子序列的最后一个数,使用优先队列存储当前数作为子序列的末尾时,子序列的长度。我们要优先选择长度较短的子序列,因此使用小根堆。

遍历数组 nums,对于当前遍历到的数 num,如果 num 不能加入到任何子序列中,那么我们就创建一个新的子序列,长度为 1;否则,我们就将 num 加入到某个子序列中,具体的子序列是哪个呢?我们可以从 num - 1 对应的子序列中取出一个长度最短的子序列,将 num 加入到该子序列中,然后将该子序列的最后一个数更新为 num,同时将该子序列的长度加 1。

如果我们遍历完数组 nums,优先队列中所有的子序列的长度都不小于 3,那么我们就可以将数组 nums 分割成若干个子序列,否则,我们就无法将数组 nums 分割成若干个子序列。

时间复杂度 O(nlogn)O(n\log n),其中 nn 是数组 nums 的长度。空间复杂度 O(n)O(n)

1
2
3
4
5
6
7
8
9
10
class Solution:
    def isPossible(self, nums: List[int]) -> bool:
        d = defaultdict(list)
        for v in nums:
            if h := d[v - 1]:
                heappush(d[v], heappop(h) + 1)
            else:
                heappush(d[v], 1)
        return all(not v or v and v[0] > 2 for v in d.values())
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Candidate is familiar with greedy strategies and hash map usage.

  • question_mark

    Candidate shows understanding of array scanning techniques and subsequence formation.

  • question_mark

    Candidate demonstrates the ability to optimize solutions using map-based lookups.

warning

常见陷阱

外企场景
  • error

    Not checking for valid subsequences before creating new ones.

  • error

    Incorrectly updating the hash map, causing invalid subsequences.

  • error

    Forgetting to handle cases where no subsequence can be formed for certain elements.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow subsequences of length 2 or more instead of 3.

  • arrow_right_alt

    Introduce additional constraints such as distinct integers in the array.

  • arrow_right_alt

    Alter the problem to find the smallest subsequence possible instead of focusing on consecutive subsequences.

help

常见问题

外企场景

分割数组为连续子序列题解:数组·哈希·扫描 | LeetCode #659 中等