当前位置:网站首页>Stack Title: nesting depth of valid parentheses
Stack Title: nesting depth of valid parentheses
2022-07-07 07:05:00 【Great Cherny】
List of articles
subject
Title and provenance
title : Nesting depth of valid parentheses
Source :1111. Nesting depth of valid parentheses
difficulty
6 level
Title Description
requirement
A string is a valid bracket string , If and only if the string contains only "(" \texttt{"("} "(" and ")" \texttt{")"} ")", And meet one of the following conditions :
- The string is an empty string ;
- The string can be written as AB \texttt{AB} AB( A \texttt{A} A And B \texttt{B} B String connection ), among A \texttt{A} A and B \texttt{B} B Are valid bracket strings ;
- The string can be written as (A) \texttt{(A)} (A), among A \texttt{A} A Is a valid parenthesis string .
For valid bracket strings S \texttt{S} S, Its nesting depth depth(S) \texttt{depth(S)} depth(S) The definition is as follows :
- depth("") = 0 \texttt{depth("")} = 0 depth("")=0;
- depth(A + B) = max(depth(A), depth(B)) \texttt{depth(A + B) = max(depth(A), depth(B))} depth(A + B) = max(depth(A), depth(B)), among A \texttt{A} A and B \texttt{B} B Are valid bracket strings ;
- depth("(" + A + ")") = 1 + depth(A) \texttt{depth("(" + A + ")") = 1 + depth(A)} depth("(" + A + ")") = 1 + depth(A), among A \texttt{A} A Is a valid parenthesis string .
for example , "" \texttt{""} ""、 "()()" \texttt{"()()"} "()()" and "()(()())" \texttt{"()(()())"} "()(()())" Is a valid bracket string ( The nesting depth is 0 \texttt{0} 0、 1 \texttt{1} 1 and 2 \texttt{2} 2), ")(" \texttt{")("} ")(" and "(()" \texttt{"(()"} "(()" Is not a valid parenthesis string .
Given a valid bracket string seq \texttt{seq} seq, Divide it into two disjoint subsequences A \texttt{A} A and B \texttt{B} B, bring A \texttt{A} A and B \texttt{B} B Is a valid bracket string , And A.length + B.length = seq.length \texttt{A.length + B.length = seq.length} A.length + B.length = seq.length.
Choose any A \texttt{A} A and B \texttt{B} B bring max(depth(A), depth(B)) \texttt{max(depth(A), depth(B))} max(depth(A), depth(B)) The minimum value of .
Returns an array of answer \texttt{answer} answer( The length is seq.length \texttt{seq.length} seq.length), Express choice A \texttt{A} A and B \texttt{B} B The coding : If seq[i] \texttt{seq[i]} seq[i] among A \texttt{A} A be answer[i] = 0 \texttt{answer[i] = 0} answer[i] = 0, otherwise answer[i] = 1 \texttt{answer[i] = 1} answer[i] = 1. If there are multiple answers that meet the requirements , Just return any one of them .
Example
Example 1:
Input : seq = "(()())" \texttt{seq = "(()())"} seq = "(()())"
Output : [0,1,1,1,1,0] \texttt{[0,1,1,1,1,0]} [0,1,1,1,1,0]
Example 2:
Input : seq = "()(())()" \texttt{seq = "()(())()"} seq = "()(())()"
Output : [0,0,0,1,1,0,1,1] \texttt{[0,0,0,1,1,0,1,1]} [0,0,0,1,1,0,1,1]
Data range
- 1 ≤ seq.size ≤ 10000 \texttt{1} \le \texttt{seq.size} \le \texttt{10000} 1≤seq.size≤10000
solution
Ideas and algorithms
Before considering dividing the valid bracket string into two disjoint subsequences , First, we need to consider the nesting depth of each bracket . Valid parenthesis string , Each left parenthesis has a matching right parenthesis , The nesting depth of a pair of matching left and right parentheses is the same .
To calculate the nesting depth of each bracket , You can use the stack to store the left parenthesis , Get the nesting depth according to the number of elements in the stack . Traverse valid bracket strings from left to right seq \textit{seq} seq, If the left bracket is encountered, the left bracket is put on the stack , If the right bracket is encountered, the left bracket at the top of the stack will be out of the stack , Indicates that the current right bracket matches the left bracket at the top of the stack . According to the definition of nesting depth , The nesting depth of the left bracket and the right bracket are calculated as follows :
For the left bracket , The number of elements in the stack after the stack operation is the nesting depth of the left bracket ;
For the right bracket , The number of elements in the stack before the stack operation is the nesting depth of the right bracket .
After clarifying the calculation method of the nesting depth of brackets , Go back to the original question , Put valid parenthesis strings seq \textit{seq} seq Divided into two disjoint subsequences A A A and B B B, Minimize the nesting depth of two subsequences . As long as the half bracket in the stack belongs to the subsequence A A A, The other half of the parentheses belong to the subsequence B B B, It can satisfy the minimum nesting depth of two subsequences . In terms of implementation , Divide the brackets of odd layers to A A A, Assign brackets of even layers to B B B that will do .
Because the nesting depth of a pair of matching left and right parentheses is the same , Therefore, a pair of matching left and right parentheses must be in the same subsequence . Thus we can see that , The number of left and right parentheses in any subsequence must be equal , And the left bracket must appear before the matching right bracket , Therefore, both subsequences are valid parenthesis strings .
Because only the left parenthesis is stored in the stack , Therefore, there is no need to really maintain a stack , Just record the nesting depth in the traversal process .
Code
class Solution {
public int[] maxDepthAfterSplit(String seq) {
int length = seq.length();
int[] answer = new int[length];
int depth = 0;
for (int i = 0; i < length; i++) {
if (seq.charAt(i) == '(') {
depth++;
answer[i] = depth % 2 == 1 ? 0 : 1;
} else {
answer[i] = depth % 2 == 1 ? 0 : 1;
depth--;
}
}
return answer;
}
}
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among n n n Is string seq \textit{seq} seq The length of . Need to traverse the string seq \textit{seq} seq once , Each time the nesting depth is updated O ( 1 ) O(1) O(1).
Spatial complexity : O ( 1 ) O(1) O(1). In addition to the return value , The space complexity used is constant .
边栏推荐
- impdp的transform参数的测试
- Use of completable future
- Please answer the questions about database data transfer
- [GNN] graphic gnn:a gender Introduction (including video)
- Tool class: object to map hump to underline underline hump
- Lvs+kept (DR mode) learning notes
- Jetpack compose is much more than a UI framework~
- The latest trends of data asset management and data security at home and abroad
- 使用net core优势/为什么使用
- 联合索引ABC的几种索引利用情况
猜你喜欢
Lvs+kept (DR mode) learning notes
化工园区危化品企业安全风险智能化管控平台建设四大目标
MySQL的主从复制原理
How DHCP router works
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
毕业设计游戏商城
7天零基础能考证HCIA吗?华为认证系统学习路线分享
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
[noi simulation] regional division (conclusion, structure)
Basic process of network transmission using tcp/ip four layer model
随机推荐
服装门店如何盈利?
根据IP获取地市
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
Unable to debug screen program with serial port
多学科融合
JESD204B时钟网络
MOS tube parameters μ A method of Cox
.net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
Lvs+kept (DR mode) learning notes
readonly 只读
大促过后,销量与流量兼具,是否真的高枕无忧?
什么情况下考虑分库分表
剑指offer-高质量的代码
Brand · consultation standardization
JWT的基础介绍
企業如何進行數據治理?分享數據治理4個方面的經驗總結
毕业设计游戏商城
Big coffee gathering | nextarch foundation cloud development meetup is coming
$refs:组件中获取元素对象或者子组件实例:
Redhat5 installing vmware tools under virtual machine