ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度

2021-05-09 23:03:57  阅读:268  来源: 互联网

标签:Parentheses VPS seq level res Two 括号 depth 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 题目讲解汇总(持续更新中...)

标签:Parentheses,VPS,seq,level,res,Two,括号,depth,Strings
来源: https://www.cnblogs.com/grandyang/p/14749193.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有