[LeetCode] 1111. 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 (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 depth depth(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.

Example 1:

Input: seq = "(()())"
Output: [0,1,1,1,1,0]

Example 2:

Input: seq = "()(())()"
Output: [0,0,0,1,1,0,1,1]

这道题给了一个合法的括号字符串,定义了一种括号深度,就是最深的括号嵌套层数。现在让将这个括号字符串拆分成为两个合法的括号字符串,且二者之中的较大深度最小,就是说要尽可能让二者的深度相同。返回数组中,用0和1来区分不同的字符串。既然是尽可能的平均拆分,那么拆分后的每个字符串的深度一定不会超过原来的一半,就是说有嵌套括号时就要平均分配给两个字符串。什么时候会有嵌套括号呢,就比如 "(())" 这种,要拆分为 "()" 和 "()",而对于没有嵌套括号的,比如 "()()()" 这种,可以都放到一个字符串中都没问题。所以问题的关键就是对于连续的左括号,要将其平均的分配到不同的字符串中。所以处理的方法就是使用一个 level 变量,初始化为0,然后遍历给定括号字符串,若遇到了左括号,则 level 对2取余,将结果存入 res 中,为了避免连续左括号加入同一个组,将 level 自增1。这样接下来又遇到左括号时,就可以加进不同的组,若接下来遇到右括号了,则应该先给 level 自增1,再对2取余,这样就可以跟前一个左括号划分到同一个组中了,参见代码如下:


解法一:

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {        
        int n = seq.size(), level = 0;
        vector<int> res(n);
        for (int i = 0; i < n; ++i) {
            if (seq[i] == '(') {
                res[i] = level++ % 2;
            } else {
                res[i] = ++level % 2;
            }
        }
        return res;
    }
};

下面这种写法更加的简洁,但其实思路都是一样的,只不过用i代替了上面的 level 变量的作用了,参见代码如下:


解法二:

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {        
        vector<int> res(seq.size());
        for (int i = 0; i < seq.size(); ++i) {
            res[i] = seq[i] == '(' ? i & 1 : (1 - i & 1);
        }
        return res;
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/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 题目讲解汇总(持续更新中...)

上一篇:20 valid parentheses


下一篇:PCB的整个加工流程