You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 (A concatenated with B), where A and B are VPS's, or
It can be written as (A), where A is a VPS.
We can similarly define the nesting depthdepth(S) of any VPS S as follows:
depth("") = 0
depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's
depth("(" + A + ")") = 1 + depth(A), where A is 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.
A string is a valid parentheses string (denoted VPS) if and only if it consists of
"("
and")"
characters only, and:AB
(A
concatenated withB
), whereA
andB
are VPS's, or(A)
, whereA
is a VPS.We can similarly define the nesting depth
depth(S)
of any VPSS
as follows:depth("") = 0
depth(A + B) = max(depth(A), depth(B))
, whereA
andB
are VPS'sdepth("(" + A + ")") = 1 + depth(A)
, whereA
is 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
andB
, such thatA
andB
are VPS's (andA.length + B.length = seq.length
).Now choose any such
A
andB
such thatmax(depth(A), depth(B))
is the minimum possible value.Return an
answer
array (of lengthseq.length
) that encodes such a choice ofA
andB
:answer[i] = 0
ifseq[i]
is part ofA
, elseanswer[i] = 1
. Note that even though multiple answers may exist, you may return any of them.Example 1:
Example 2:
这道题给了一个合法的括号字符串,定义了一种括号深度,就是最深的括号嵌套层数。现在让将这个括号字符串拆分成为两个合法的括号字符串,且二者之中的较大深度最小,就是说要尽可能让二者的深度相同。返回数组中,用0和1来区分不同的字符串。既然是尽可能的平均拆分,那么拆分后的每个字符串的深度一定不会超过原来的一半,就是说有嵌套括号时就要平均分配给两个字符串。什么时候会有嵌套括号呢,就比如 "(())" 这种,要拆分为 "()" 和 "()",而对于没有嵌套括号的,比如 "()()()" 这种,可以都放到一个字符串中都没问题。所以问题的关键就是对于连续的左括号,要将其平均的分配到不同的字符串中。所以处理的方法就是使用一个 level 变量,初始化为0,然后遍历给定括号字符串,若遇到了左括号,则 level 对2取余,将结果存入 res 中,为了避免连续左括号加入同一个组,将 level 自增1。这样接下来又遇到左括号时,就可以加进不同的组,若接下来遇到右括号了,则应该先给 level 自增1,再对2取余,这样就可以跟前一个左括号划分到同一个组中了,参见代码如下:
解法一:
下面这种写法更加的简洁,但其实思路都是一样的,只不过用i代替了上面的 level 变量的作用了,参见代码如下:
解法二:
Github 同步地址:
#1111
类似题目:
Maximum Nesting Depth of the Parentheses
参考资料:
https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/
https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/discuss/328841/JavaC%2B%2BPython-O(1)-Extra-Space-Except-Output
https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/discuss/358419/Confused-by-this-problem-I-was-too-%3A(-Here-is-how-it-became-crystal-clear...
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: