当前位置 : 首页 » 文章分类 :  算法  »  LeetCode.1111.Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度

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

题目描述

1111 Maximum Nesting Depth of Two Valid Parentheses Strings
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 (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]

Constraints:
1 <= seq.size <= 10000


解题过程

题目很抽象,比较难理解,大致意思是把一个 有效括号串 拆分为两部分,A和B,使得这两个部分的嵌套深度尽可能相同。
这个拆分指的是从中选择出两个子序列,不要求连续,只需保持相对位置不变即可,且两个子序列加起来等于原串。
返回的结果数组是一个划分,所有0值元素是一组,所有1值元素是另外一组。

为了使 max(depth(A), depth(B)) 尽可能小,应该使两组括号的嵌套深度尽可能相同,所以我们应该把嵌套深度大于等于2的都均分为到A B两组中,比如 (((()))) 是一个嵌套4层的串,拆分为 (())(()) 后,每组深度为2,深度差最小。

可行的一种方案是,利用栈来统计嵌套深度,把每个元素的深度标识出来
例如

括号序列   ( ( ) ( ( ) ) ( ) )
下标编号   0 1 2 3 4 5 6 7 8 9
嵌套深度   1 2 2 2 3 3 2 2 2 1

然后采用一个非常通用的均分策略,嵌套深度为奇数的划分到A中,嵌套深度为偶数的划分到B中。
由于所给括号串一定是合法的,所以并不需要一个真正的栈,只需一个栈深度计数器统计即可。

时间复杂度 O(n),空间复杂度 O(1)

private static class SolutionV2020 {
    public int[] maxDepthAfterSplit(String seq) {
        if (null == seq || 0 == seq.length()) {
            return null;
        }
        int[] res = new int[seq.length()];
        int stackSize = 0;
        for (int i = 0; i < seq.length(); i++) {
            if (seq.charAt(i) == '(') {
                // 左括号入栈,栈深度加1
                stackSize++;
                // 栈深度是奇数的,归为第一组,偶数的归为第二组
                res[i] = stackSize % 2 == 1 ? 0 : 1;
            } else {
                // 栈深度是奇数的,归为第一组,偶数的归为第二组
                res[i] = stackSize % 2 == 1 ? 0 : 1;
                // 右括号和栈顶抵消,栈深度减1
                stackSize--;
            }
        }
        return res;
    }
}

GitHub代码

algorithms/leetcode/leetcode/_1111_NestingDepthOfValidParentheses.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_1111_NestingDepthOfValidParentheses.java


上一篇 JUnit

下一篇 LeetCode.912.Sort an Array 排序数组

阅读
评论
773
阅读预计4分钟
创建日期 2020-04-01
修改日期 2020-04-01
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论