当前位置:网站首页>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 .
边栏推荐
- Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
- . Net core accesses uncommon static file types (MIME types)
- .net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
- 使用net core优势/为什么使用
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
- Mysql---- import and export & View & Index & execution plan
- 【NOI模拟赛】区域划分(结论,构造)
- The startup of MySQL installed in RPM mode of Linux system failed
- 毕业设计游戏商城
- Basic introduction of JWT
猜你喜欢
随机推荐
Use of completable future
品牌电商如何逆势增长?在这里预见未来!
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
网络基础 —— 报头、封装和解包
根据IP获取地市
Advantages of using net core / why
How to model and simulate the target robot [mathematical / control significance]
华为机试题素数伴侣
Prime partner of Huawei machine test questions
精准时空行程流调系统—基于UWB超高精度定位系统
2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
DHCP路由器工作原理
Test of transform parameters of impdp
A slow SQL drags the whole system down
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案
Sqlserver multithreaded query problem
impdp的transform参数的测试
多学科融合