PROBLEM LINK:
Editorialist: Adithya Dsilva
DIFFICULTY:
SIMPLE
PREREQUISITES:
PROBLEM:
You are given a well-bracketed sequence. You have to determine:
- The maximum nesting depth of the sequence along with the first opening bracket at this depth.
The nesting depth tells us the maximum number of levels of inner matched brackets enclosed within outer matched brackets. The nesting depth of()
and()()()
is 1, the nesting depth of(())
and()(())
is 2 and the nesting depth of((()))
is 3. - The maximum number of symbols between any pair of matched brackets (inclusive of the outer brackets) and the first position where this occurs – the position of the first opening bracket of this segment.
EXPLANATION:
\ast Outermost brackets refer to brackets which are not enclosed within any other bracket pair.
KEY OBSERVATIONS:
- An opening bracket
(
at any index increases the depth of the sequence by 1Proof
The depth of a bracket refers to the number of bracket pairs it is enclosed within (including itself, if its a
(
bracket). Thus, any(
bracket has a depth of 1 + the depth of its parent bracket, as it’s enclosed within all the brackets its parent is enclosed within (+1 because the current(
bracket also contributes to the depth). - A closing bracket
)
at any index decreases the depth of the sequence by 1Proof
Any bracket right after a
)
bracket would mean its no longer enclosed within this corresponding bracket pair. But however it still is enclosed within all the brackets the former bracket was enclosed within (since we have come out of only 1 nesting). Thus the depth of this bracket is the depth of the previous matched bracket (which the current bracket is not enclosed within) - 1. - If the depth at any point in the sequence becomes 0, it means that the next bracket is an outermost bracket in the sequence.
Proof
If the depth is 0, it means that every
(
bracket has been matched by their corresponding)
bracket, implying that the next bracket is an outer-most bracket. This resets the depth and the count of symbols between outermost brackets. - The count of symbols between outermost brackets is greater than the count of symbols between bracket-pairs inside the parent bracket.
Proof
An outermost bracket pair includes all the symbols enclosed by brackets nested inside it + 2 (since count of symbols within a bracket pair also includes these outer brackets)
Let us use the above points to find the solution. We are going to start with finding the Max Depth of the sequence.
- Iterate through the array.
-
Depth
refers to the depth of the current bracket in the sequence (remember we are iterating through the array). - Using points (1) and (2) from above,
Depth++
when we encounter a(
andDepth--
when we encounter a)
. - We maintain a variable
max_Depth
, that determines the maximum depth that we have found so far in the iteration. - Now, if
Depth
>max_Depth
, we updatemax_Depth
to value ofDepth
. We also updateDepthLoc
here to the current index in the iteration.DepthLoc
refers to the location of the first bracket achieving this depth.
Note the > and not \ge, as we want to find the first index that hasmax_Depth
depth. Using the latter would result in returning the last index (if multiple indexes exist) with depthmax_Depth
.
The max depth is thus max_Depth
and the first index is DepthLoc
.
Similar approach can be used to determine the Max Count.
- Iterate through the array.
-
Cnt
refers to the count of symbols enclosed within the outermost brackets (or topmost parent brackets) of the current bracket. - From point (3) above, we realise that until the depth of a sequence becomes 0,
Cnt
keeps increasing by 1, for every bracket we iterate through. Thus, ifDepth != 0
,Cnt++
.
But ifDepth == 0
, what happens? It means we have crossed the closing bracket of the outermost opening bracket in the sequence, and are beginning with another outermost bracket. ThusCnt
is reset to 0. -
max_Cnt
is determined similarly, as done in determiningmax_Depth
(Using a variablemax_Cnt
and using the > property). - How do we determine the first index, with
max_Cnt
symbols enclosed within? Simple! Every time we encounter an outermost(
opening bracket (whenDepth == 0
), we set a variableBracketStartIndex
to this index.
CntLoc
can be used to determine the answer, and is updated similarly, like howDepthLoc
was updated.
Using the above steps, we can determine the solution!
SOLUTION SNIPPET:
int N; cin >> N;
int Depth = 0, max_Depth = 0, DepthLoc = 0; //Depth variables
int Cnt = 0, max_Cnt = 0, CntLoc = 0; //Count variables
int BracketStartIndex = 0; //Holds the outermost bracket in which,
//the current bracket is contained
for(int x = 1; x <= N; x++){
if(!Depth){ //If Depth = 0.
Cnt = 0; //Reset count. Refer point 3 above.
BracketStartIndex = x; //This bracket is the outermost bracket
//in the sequence to follow!
}
int i; cin >> i; //Input of current bracket in the sequence
Cnt++; //Increase count. Any bracket (opening or closing) adds
//to the count of the outermost bracket.
if(i == 1) Depth++;//If opening bracket, depth increases
else Depth--; //Else, depth decreases. Refer point 2.
if(Depth > max_Depth){ //Depth is greater than prev known depth
DepthLoc = x; //Set current index as first index with this depth
max_Depth = Depth; //Update max_depth value
}
if(Cnt > max_Cnt){ //Count is greater than prev known count
CntLoc = BracketStartIndex; //Set outermost bracket in sequence
//as the first bracket with this count
max_Cnt = Cnt; //Update max_Cnt value
}
}
cout << max_Depth << DepthLoc << max_Cnt << CntLoc << endl;
//Remember to add spaces between each value in the output!
TIME COMPLEXITY:
Since only one iteration is made through the entire array, the time complexity of the above code is O(N).
SOLUTIONS:
Editorialists solution can be found here.
Did you like the editorial? Do you have any other approaches to the problem? Any suggestions? Comment them below!
Experimental: For better evaluation of my editorials, I request all readers to rate the editorial (on a scale from 1-5, where 5 represents an awesome editorial)
- 1
- 2
- 3
- 4
- 5
0 voters
Also don’t forget to up-vote this post if you liked it !