LeetCode 题解工作台

检查数组是否存在有效划分

给你一个下标从 0 开始的整数数组 nums ,你必须将数组划分为一个或多个 连续 子数组。 如果获得的这些子数组中每个都能满足下述条件 之一 ,则可以称其为数组的一种 有效 划分: 子数组 恰 由 2 个相等元素组成,例如,子数组 [2,2] 。 子数组 恰 由 3 个相等元素组成,例如,子数组 …

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 状态·转移·动态规划

bolt

答案摘要

我们设计一个函数 ,表示从下标 开始是否存在一种有效划分。那么答案就是 。 函数 的执行过程如下:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的整数数组 nums ,你必须将数组划分为一个或多个 连续 子数组。

如果获得的这些子数组中每个都能满足下述条件 之一 ,则可以称其为数组的一种 有效 划分:

  1. 子数组 2 个相等元素组成,例如,子数组 [2,2]
  2. 子数组 3 个相等元素组成,例如,子数组 [4,4,4]
  3. 子数组 3 个连续递增元素组成,并且相邻元素之间的差值为 1 。例如,子数组 [3,4,5] ,但是子数组 [1,3,5] 不符合要求。

如果数组 至少 存在一种有效划分,返回 true ,否则,返回 false

 

示例 1:

输入:nums = [4,4,4,5,6]
输出:true
解释:数组可以划分成子数组 [4,4] 和 [4,5,6] 。
这是一种有效划分,所以返回 true 。

示例 2:

输入:nums = [1,1,1,2]
输出:false
解释:该数组不存在有效划分。

 

提示:

  • 2 <= nums.length <= 105
  • 1 <= nums[i] <= 106
lightbulb

解题思路

方法一:记忆化搜索

我们设计一个函数 dfs(i)dfs(i),表示从下标 ii 开始是否存在一种有效划分。那么答案就是 dfs(0)dfs(0)

函数 dfs(i)dfs(i) 的执行过程如下:

  • 如果 ini \ge n,返回 truetrue
  • 如果 iii+1i+1 下标的元素相等,那么可以选择将 iii+1i+1 作为一个子数组,递归调用 dfs(i+2)dfs(i+2)
  • 如果 ii, i+1i+1i+2i+2 下标的元素相等,那么可以选择将 ii, i+1i+1i+2i+2 作为一个子数组,递归调用 dfs(i+3)dfs(i+3)
  • 如果 ii, i+1i+1i+2i+2 下标的元素依次递增 11,那么可以选择将 ii, i+1i+1i+2i+2 作为一个子数组,递归调用 dfs(i+3)dfs(i+3)
  • 如果上述情况都不满足,返回 falsefalse,否则返回 truetrue

即:

dfs(i)=OR{true,indfs(i+2),i+1<n and nums[i]=nums[i+1]dfs(i+3),i+2<n and nums[i]=nums[i+1]=nums[i+2]dfs(i+3),i+2<n and nums[i+1]nums[i]=1 and nums[i+2]nums[i+1]=1dfs(i) = \textit{OR} \begin{cases} true,&i \ge n\\ dfs(i+2),&i+1 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1]\\ dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i] = \textit{nums}[i+1] = \textit{nums}[i+2]\\ dfs(i+3),&i+2 < n\ \textit{and}\ \textit{nums}[i+1] - \textit{nums}[i] = 1\ \textit{and}\ \textit{nums}[i+2] - \textit{nums}[i+1] = 1 \end{cases}

为了避免重复计算,我们使用记忆化搜索的方法。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def validPartition(self, nums: List[int]) -> bool:
        @cache
        def dfs(i: int) -> bool:
            if i >= n:
                return True
            a = i + 1 < n and nums[i] == nums[i + 1]
            b = i + 2 < n and nums[i] == nums[i + 1] == nums[i + 2]
            c = (
                i + 2 < n
                and nums[i + 1] - nums[i] == 1
                and nums[i + 2] - nums[i + 1] == 1
            )
            return (a and dfs(i + 2)) or ((b or c) and dfs(i + 3))

        n = len(nums)
        return dfs(0)
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Candidates should demonstrate knowledge of dynamic programming state transitions and handling subarrays.

  • question_mark

    Look for candidates who can explain how smaller subarrays lead to valid partitions and how this problem is reduced to checking smaller subproblems.

  • question_mark

    Candidates should be able to identify the base case and how transitions occur from one state to another.

warning

常见陷阱

外企场景
  • error

    Forgetting to initialize the base case properly can lead to incorrect results.

  • error

    Overcomplicating the problem by not recognizing the simple state transitions between valid subarrays.

  • error

    Failing to handle the edge case of very small arrays properly can lead to unexpected outcomes.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Changing the condition of valid subarrays from equality to some other rule.

  • arrow_right_alt

    Introducing a constraint that subarrays must not only be valid but also sorted in a particular order.

  • arrow_right_alt

    Extending the problem to check for multiple valid partitions rather than just one.

help

常见问题

外企场景

检查数组是否存在有效划分题解:状态·转移·动态规划 | LeetCode #2369 中等