原文链接: https://leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings
英文原文
A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:
- It is the empty string, or
- It can be written as
AB(Aconcatenated withB), whereAandBare VPS's, or - It can be written as
(A), whereAis a VPS.
We can similarly define the nesting depth depth(S) of any VPS S as follows:
depth("") = 0depth(A + B) = max(depth(A), depth(B)), whereAandBare VPS'sdepth("(" + A + ")") = 1 + depth(A), whereAis a VPS.
For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.
Given a VPS seq, split it into two disjoint subsequences A and B, such that A and B are VPS's (and A.length + B.length = seq.length).
Now choose any such A and B such that max(depth(A), depth(B)) is the minimum possible value.
Return an answer array (of length seq.length) that encodes such a choice of A and B: answer[i] = 0 if seq[i] is part of A, else answer[i] = 1. Note that even though multiple answers may exist, you may return any of them.
Example 1:
Input: seq = "(()())" Output: [0,1,1,1,1,0]
Example 2:
Input: seq = "()(())()" Output: [0,0,0,1,1,0,1,1]
Constraints:
1 <= seq.size <= 10000
中文题目
有效括号字符串 定义:对于每个左括号,都能找到与之对应的右括号,反之亦然。详情参见题末「有效括号字符串」部分。
嵌套深度 depth 定义:即有效括号字符串嵌套的层数,depth(A) 表示有效括号字符串 A 的嵌套深度。详情参见题末「嵌套深度」部分。
有效括号字符串类型与对应的嵌套深度计算方法如下图所示:

给你一个「有效括号字符串」 seq,请你将其分成两个不相交的有效括号字符串,A 和 B,并使这两个字符串的深度最小。
- 不相交:每个
seq[i]只能分给A和B二者中的一个,不能既属于A也属于B。 A或B中的元素在原字符串中可以不连续。A.length + B.length = seq.length- 深度最小:
max(depth(A), depth(B))的可能取值最小。
划分方案用一个长度为 seq.length 的答案数组 answer 表示,编码规则如下:
answer[i] = 0,seq[i]分给A。answer[i] = 1,seq[i]分给B。
如果存在多个满足要求的答案,只需返回其中任意 一个 即可。
示例 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,而")("和"(()"都不是有效括号字符串。
通过代码
高赞题解
解题思路:
这道题题意说的其实有点啰嗦的。首先你要知道有效括号的意思,一句话概括就是每个左括号都可以找到在它右边的与其对应的右括号。不知道有效括号的概念的,或者没有刷过判断有效括号题目的,建议可以移步这里: 有效的括号。
题面最后 answer 的意思就是,为 0 的部分对应 seq 的括号是 A 字符串,为 1 的部分对应 seq 的括号是 B 字符串。
示例 1:
输入:seq = "(()())"
输出:[0,1,1,1,1,0]
answer 的意思是下面这个样子。红色部分是 A 串,蓝色部分是 B 串。
{:height=”30%” width=”30%”}
示例 2:
输入:seq = "()(())()"
输出:[0,0,0,1,1,0,1,1]
对应的 answer 的图示:
{:height=”30%” width=”30%”}
题面也说了 answer 的答案是不唯一的,下面这样也是可以的:
{:height=”30%” width=”30%”}
下面说做法:
我假设你已经做过上面的题目了,知道需要用栈辅助判断。题面中的 depth 其实就是栈的最大深度。“你需要从中选出任意一组有效括号字符串 A 和 B,使 max(depth(A), depth(B)) 的可能取值最小”。这句话其实相当于让 A 字符串和 B 字符串的 depth 尽可能的接近。为什么呢?因为 seq 对应的栈上,每个左括号都对应一个深度,而这个左括号,要么是 A 的,要么是 B 的。所以,栈上的左括号只要按奇偶分配给A和B就可以啦!时间复杂度很明显是 $O(n)$ 的,空间复杂度也是 $O(n)$(如果算返回的变量的话)。
[]public class Solution { public int[] maxDepthAfterSplit(String seq) { int[] ans = new int [seq.length()]; int idx = 0; for(char c: seq.toCharArray()) { ans[idx++] = c == '(' ? idx & 1 : ((idx + 1) & 1); } return ans; } }
以上谢谢大家,求赞求赞求赞!
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 24441 | 31686 | 77.1% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|