ParenthesesDiv2Easy
SRM 688 · 2016-04-01 · by cgy4ever
SRM 688 · 2016-04-01 · by cgy4ever · Brute Force
Problem Statement
Problem Statement
Correct parentheses sequences can be defined recursively as follows:
We can define the depth of a correct parentheses sequence recursively as follows:
Note that the depth of each correct parentheses sequence is uniquely defined using the above rules.
For example, when evaluating the depth of "()()()" it does not matter whether we take X = "()" and Y = "()()" or we take X = "()()" and Y = "()", the result will be the same in both cases.
Given aString s that is a correct parentheses sequence, calculate and return the depth of s.
- The empty string "" is a correct sequence.
- If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
- If "X" is a correct sequence, then "(X)" is a correct sequence.
- Each correct parentheses sequence can be derived using the above rules.
We can define the depth of a correct parentheses sequence recursively as follows:
- The empty string "" has depth 0.
- If the depth of "X" is x and the depth of "Y" is y then the depth of "XY" is max(x,y).
- If the depth of "X" is x then the depth of "(X)" is x+1.
Note that the depth of each correct parentheses sequence is uniquely defined using the above rules.
For example, when evaluating the depth of "()()()" it does not matter whether we take X = "()" and Y = "()()" or we take X = "()()" and Y = "()", the result will be the same in both cases.
Given a
Constraints
- s will contain between 2 and 50 characters, inclusive.
- Each character in s will be '(' or ')'.
- s will be a correct parentheses sequence.
Examples
0)
"(())" Returns: 2
The depth of "" is 0. Therefore, the depth of "()" is 1. Next, the depth of "(())" is the depth of "()" plus 1, which makes it 1+1 = 2.
1)
"()()()" Returns: 1
The depth of "()()" is the maximum of the depth of "()" and the depth of "()". In other words, it is max(1,1) = 1. The depth of "()()()" is the maximum of the depth of "()()" and the depth of "()". Hence, this also equals max(1,1) = 1.
2)
"(())()" Returns: 2
The answer is max(2, 1) = 2.
3)
"((())())(((())(()))())" Returns: 4
4)
"()" Returns: 1
Submissions are judged against all 48 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class ParenthesesDiv2Easy with a public method int getDepth(string s) · 48 test cases · 2 s / 256 MB per case