题目如下:

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或者A+1 = B 即可。用a_count和b_count分别记录A与B未配对的左括号数量。然后遍历seq,如果seq[i]为左括号,判断a_count与b_count的大小:如果a_count <= b_count ,表示这个左括号划分到A中,a_count ++;否则划分到B,b_count++。 如果seq[i]是右括号,判断a_count与b_count的大小:如果a_count >= b_count ,表示这个右括号优先于A中的左括号匹配,a_count -- ;否则与B匹配,b_count --。

代码如下:

class Solution(object):
def maxDepthAfterSplit(self, seq):
"""
:type seq: str
:rtype: List[int]
"""
res = []
a_count = 0
b_count = 0 a_s = ''
b_s = ''
for i in seq:
if i == '(':
if a_count <= b_count:
res.append(0)
a_count += 1
a_s += i
else:
res.append(1)
b_count += 1
b_s += i elif i == ')':
if a_count >= b_count:
res.append(0)
a_count -= 1
a_s += i
else:
res.append(1)
b_count -= 1
b_s += i
#print a_s,b_s
return res

【leetcode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings的更多相关文章

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目讲解 划分规则讲解 返回结果讲解 解题方法 代码 日期 题目地址:ht ...

  2. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  3. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  4. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  7. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  8. 【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  9. 【LeetCode】104 - Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. Scala的集合框架

    1.元组 定义方式:val tp=("nana',1,1.1) 特点:集合中的数据可以是不同类型的 最多只能放22个元素 取值:通过角标取值,这里的角标是从1开始的,元组名称._角标   t ...

  2. SPA(single page application)

    一.SPA的概述 SPA(single page application)单页面应用程序,在一个完成的应用或者站点中,只有一个完整的html页面,这个页面有一个容器,可以把需要加载的代码片段插入到该容 ...

  3. cocos2dx基础篇(20) 扩展动作CCGridAction

    [3.x]     (1)去掉"CC" [CCGridAction] CCGridAction有两个子类:CCGrid3DAction.CCTiledGrid3DAction.而我 ...

  4. 【Linux开发】将cmd中命令输出保存为TXT文本文件

    将cmd中命令输出保存为TXT文本文件 在网上看到一篇名为:"[转载]如何将cmd中命令输出保存为TXT文本文件" 例如:将Ping命令的加长包输出到D盘的ping.txt文本文件 ...

  5. python+selenium下弹窗alter对象处理01

    alt.accept() :                            等同于单击“确认”或者“OK” alt.dismiss() :                            ...

  6. js swich

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. HDU 1231 题解

    题面: 最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...

  8. 19: vue项目使用整理

    1.1  axios 基本用法 安装:npm install axios -S                   # 也可直接下载axios.min.js文件 1.axios借助Qs对提交数据进行序 ...

  9. 通过编写串口助手工具学习MFC过程——(五)添加CheckBox复选框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  10. 编写的Java第一个程序

    没什么好介绍的,嘻嘻 package head; public class ee { public static void main(String[] args) { System.out.print ...