LeetCode 题解工作台
有效括号的嵌套深度
如果一个字符串仅由字符 "(" 和 ")" 组成,并且满足以下条件,则称为有效括号字符串(VPS): 它是空字符串,或 它可以表示为 AB ( A 连接 B ),其中 A 和 B 都是VPS,或者 它可以表示为 (A) ,其中 A 是一个 VPS。 我们可以类似地定义任何 VPS S 的嵌套深度 d…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 栈·状态
答案摘要
我们用一个变量 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。 遍历字符串 ,更新 的值。如果 为奇数,我们将当前的左括号分给 ,否则分给 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路
题目描述
如果一个字符串仅由字符 "(" 和 ")" 组成,并且满足以下条件,则称为有效括号字符串(VPS):
- 它是空字符串,或
- 它可以表示为
AB(A连接B),其中A和B都是VPS,或者 - 它可以表示为
(A),其中A是一个 VPS。
我们可以类似地定义任何 VPS S 的嵌套深度 depth(S) 如下:
depth("") = 0depth(A + B) = max(depth(A), depth(B)),其中A和B都是 VPSdepth("(" + A + ")") = 1 + depth(A),其中A是一个 VPS。
例如,"","()()" 和 "()(()())" 都是 VPS(嵌套深度 0,1 和 2),并且 ")(" 和 "(()" 不是 VPS。
给定一个 VPS 序列,将其拆分成两个不相交的子序列 A 和 B,使得 A 和 B 都是 VPS(且 A.length + B.length = seq.length)。这些子序列不一定是连续的。
例如,对于序列 123456789,一种可能的拆分是:
-
A = {1, 3, 5, 7, 9}, -
B = {2, 4, 6, 8}。 -
这对应于输出
[0, 1, 0, 1, 0, 1, 0, 1, 0],其中 0 表示属于A,1 表示属于B。
现在选择 任意 这样的 A 和 B,使得 max(depth(A), depth(B)) 的值是最小的。
返回一个 answer 数组(长度为 seq.length),该数组编码了 A 和 B 的选择:如果 seq[i] 是 A 的一部分则 answer[i] = 0,否则 answer[i] = 1。请注意,尽管可能存在多种答案,但你可以返回其中任意一种。
示例 1:
输入:seq = "(()())" 输出:[0,1,1,1,1,0]
示例 2:
输入:seq = "()(())()" 输出:[0,0,0,1,1,0,1,1] 解释:本示例答案不唯一。 按此输出 A = "()()", B = "()()", max(depth(A), depth(B)) = 1,它们的深度最小。 像 [1,1,1,0,0,1,1,1],也是正确结果,其中 A = "()()()", B = "()", max(depth(A), depth(B)) = 1 。
提示:
1 < seq.size <= 10000
有效括号字符串:
仅由"("和")"构成的字符串,对于每个左括号,都能找到与之对应的右括号,反之亦然。 下述几种情况同样属于有效括号字符串: 1. 空字符串 2. 连接,可以记作AB(A与B连接),其中A和B都是有效括号字符串 3. 嵌套,可以记作(A),其中A是有效括号字符串
嵌套深度:
类似地,我们可以定义任意有效括号字符串s的 嵌套深度depth(S): 1.s为空时,depth("") = 02. s为A与B连接时,depth(A + B) = max(depth(A), depth(B)),其中A和B都是有效括号字符串3. s为嵌套情况,depth("(" + A + ")") = 1 + depth(A),其中A是有效括号字符串 例如:"","()()",和"()(()())"都是有效括号字符串,嵌套深度分别为 0,1,2,而")("和"(()"都不是有效括号字符串。
解题思路
方法一:贪心
我们用一个变量 维护当前括号的平衡度,也就是左括号的数量减去右括号的数量。
遍历字符串 ,更新 的值。如果 为奇数,我们将当前的左括号分给 ,否则分给 。
时间复杂度 ,其中 是字符串 的长度。忽略答案数组的空间消耗,空间复杂度 。
class Solution:
def maxDepthAfterSplit(self, seq: str) -> List[int]:
ans = [0] * len(seq)
x = 0
for i, c in enumerate(seq):
if c == "(":
ans[i] = x & 1
x += 1
else:
x -= 1
ans[i] = x & 1
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Check if the candidate understands stack-based state management.
- question_mark
Evaluate how well the candidate optimizes space and time complexity.
- question_mark
Ensure the candidate handles edge cases like consecutive closing parentheses or unbalanced parentheses correctly.
常见陷阱
外企场景- error
Forgetting to reset the depth value after encountering a closing parenthesis.
- error
Incorrectly handling unbalanced parentheses strings.
- error
Not accounting for deeply nested parentheses and overestimating depth at certain points.
进阶变体
外企场景- arrow_right_alt
Implementing a solution without a stack, using a different data structure or approach.
- arrow_right_alt
Allowing for invalid parentheses strings and handling errors or exceptions.
- arrow_right_alt
Modifying the problem to return a boolean indicating if the parentheses are valid, instead of tracking depth.