LeetCode 题解工作台
找出所有稳定的二进制数组 I
给你 3 个正整数 zero , one 和 limit 。 一个 二进制数组 arr 如果满足以下条件,那么我们称它是 稳定的 : 0 在 arr 中出现次数 恰好 为 zero 。 1 在 arr 中出现次数 恰好 为 one 。 arr 中每个长度超过 limit 的 子数组 都 同时 包含 …
2
题型
6
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
我们设计一个函数 $\textit{dfs}(i, j, k)$ 表示还剩下 个 和 个 且接下来待填的数字是 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 $\textit{dfs}(\textit{zero}, \textit{one}, 0) + \textit{dfs}(\textit{zero}, \textit{one}, 1)$。 函数 $\textit{df…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你 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
解题思路
方法一:记忆化搜索
我们设计一个函数 表示还剩下 个 和 个 且接下来待填的数字是 的情况下,满足题目条件的稳定二进制数组的个数。那么答案就是 。
函数 的计算过程如下:
- 如果 或 ,返回 。
- 如果 ,那么当 且 时返回 ,否则返回 。
- 如果 ,那么当 且 时返回 ,否则返回 。
- 如果 ,我们考虑前一个数字是 的情况 和前一个数字是 的情况 ,如果前一个数是 ,那么有可能使得子数组中有超过 个 ,即不允许出现倒数第 个数是 的情况,所以我们要减去这种情况,即 。
- 如果 ,我们考虑前一个数字是 的情况 和前一个数字是 的情况 ,如果前一个数是 ,那么有可能使得子数组中有超过 个 ,即不允许出现倒数第 个数是 的情况,所以我们要减去这种情况,即 。
为了避免重复计算,我们使用记忆化搜索的方法。
时间复杂度 ,空间复杂度 。
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
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- 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.
常见陷阱
外企场景- 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.
进阶变体
外企场景- 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.