LeetCode 题解工作台

找出所有稳定的二进制数组 I

给你 3 个正整数 zero , one 和 limit 。 一个 二进制数组 arr 如果满足以下条件,那么我们称它是 稳定的 : 0 在 arr 中出现次数 恰好 为 zero 。 1 在 arr 中出现次数 恰好 为 one 。 arr 中每个长度超过 limit 的 子数组 都 同时 包含 …

category

2

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

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

bolt

答案摘要

我们设计一个函数 $\textit{dfs}(i, j, k)$ 表示还剩下 个 和 个 且接下来待填的数字是 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 $\textit{dfs}(\textit{zero}, \textit{one}, 0) + \textit{dfs}(\textit{zero}, \textit{one}, 1)$。 函数 $\textit{df…

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

给你 3 个正整数 zero ,one 和 limit 。

一个 二进制数组 arr 如果满足以下条件,那么我们称它是 稳定的

  • 0 在 arr 中出现次数 恰好 为 zero 。
  • 1 在 arr 中出现次数 恰好 为 one 。
  • arr 中每个长度超过 limit 的 子数组同时 包含 0 和 1 。

请你返回 稳定 二进制数组的 数目。

由于答案可能很大,将它对 109 + 7 取余 后返回。

 

示例 1:

输入:zero = 1, one = 1, limit = 2

输出:2

解释:

两个稳定的二进制数组为 [1,0] 和 [0,1] ,两个数组都有一个 0 和一个 1 ,且没有子数组长度大于 2 。

示例 2:

输入:zero = 1, one = 2, limit = 1

输出:1

解释:

唯一稳定的二进制数组是 [1,0,1] 。

二进制数组 [1,1,0] 和 [0,1,1] 都有长度为 2 且元素全都相同的子数组,所以它们不稳定。

示例 3:

输入:zero = 3, one = 3, limit = 2

输出:14

解释:

所有稳定的二进制数组包括 [0,0,1,0,1,1] ,[0,0,1,1,0,1] ,[0,1,0,0,1,1] ,[0,1,0,1,0,1] ,[0,1,0,1,1,0] ,[0,1,1,0,0,1] ,[0,1,1,0,1,0] ,[1,0,0,1,0,1] ,[1,0,0,1,1,0] ,[1,0,1,0,0,1] ,[1,0,1,0,1,0] ,[1,0,1,1,0,0] ,[1,1,0,0,1,0] 和 [1,1,0,1,0,0] 。

 

提示:

  • 1 <= zero, one, limit <= 200
lightbulb

解题思路

方法一:记忆化搜索

我们设计一个函数 dfs(i,j,k)\textit{dfs}(i, j, k) 表示还剩下 ii00jj11 且接下来待填的数字是 kk 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 dfs(zero,one,0)+dfs(zero,one,1)\textit{dfs}(\textit{zero}, \textit{one}, 0) + \textit{dfs}(\textit{zero}, \textit{one}, 1)

函数 dfs(i,j,k)\textit{dfs}(i, j, k) 的计算过程如下:

  • 如果 i<0i \lt 0j<0j \lt 0,返回 00
  • 如果 i=0i = 0,那么当 k=1k = 1jlimitj \leq \textit{limit} 时返回 11,否则返回 00
  • 如果 j=0j = 0,那么当 k=0k = 0ilimiti \leq \textit{limit} 时返回 11,否则返回 00
  • 如果 k=0k = 0,我们考虑前一个数字是 00 的情况 dfs(i1,j,0)\textit{dfs}(i - 1, j, 0) 和前一个数字是 11 的情况 dfs(i1,j,1)\textit{dfs}(i - 1, j, 1),如果前一个数是 00,那么有可能使得子数组中有超过 limit\textit{limit}00,即不允许出现倒数第 limit+1\textit{limit} + 1 个数是 11 的情况,所以我们要减去这种情况,即 dfs(ilimit1,j,1)\textit{dfs}(i - \textit{limit} - 1, j, 1)
  • 如果 k=1k = 1,我们考虑前一个数字是 00 的情况 dfs(i,j1,0)\textit{dfs}(i, j - 1, 0) 和前一个数字是 11 的情况 dfs(i,j1,1)\textit{dfs}(i, j - 1, 1),如果前一个数是 11,那么有可能使得子数组中有超过 limit\textit{limit}11,即不允许出现倒数第 limit+1\textit{limit} + 1 个数是 00 的情况,所以我们要减去这种情况,即 dfs(i,jlimit1,0)\textit{dfs}(i, j - \textit{limit} - 1, 0)

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

时间复杂度 O(zero×one)O(\textit{zero} \times \textit{one}),空间复杂度 O(zero×one)O(\textit{zero} \times \textit{one})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
    def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
        @cache
        def dfs(i: int, j: int, k: int) -> int:
            if i == 0:
                return int(k == 1 and j <= limit)
            if j == 0:
                return int(k == 0 and i <= limit)
            if k == 0:
                return (
                    dfs(i - 1, j, 0)
                    + dfs(i - 1, j, 1)
                    - (0 if i - limit - 1 < 0 else dfs(i - limit - 1, j, 1))
                )
            return (
                dfs(i, j - 1, 0)
                + dfs(i, j - 1, 1)
                - (0 if j - limit - 1 < 0 else dfs(i, j - limit - 1, 0))
            )

        mod = 10**9 + 7
        ans = (dfs(zero, one, 0) + dfs(zero, one, 1)) % mod
        dfs.cache_clear()
        return ans
speed

复杂度分析

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

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate's understanding of dynamic programming and state transitions.

  • question_mark

    Evaluate the candidate's ability to optimize both time and space complexity in a DP-based approach.

  • question_mark

    Assess the candidate's ability to apply the prefix sum technique to optimize counting and state transitions.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the consecutive identical digit limit while transitioning between states.

  • error

    Not using the sliding window technique to optimize space complexity in larger test cases.

  • error

    Inefficient transitions or recalculations leading to excessive time complexity.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Different limit values could be tested to assess the solution's ability to handle various stability constraints.

  • arrow_right_alt

    The number of 0's and 1's could be varied to test the candidate's ability to handle different binary array sizes.

  • arrow_right_alt

    Testing with edge cases, such as limit being 1 or 0, will assess the robustness of the candidate's solution.

help

常见问题

外企场景

找出所有稳定的二进制数组 I题解:状态·转移·动态规划 | LeetCode #3129 中等