Nesting depth effective parentheses

String Defines a valid brackets: for each of a left parenthesis, right parenthesis corresponding thereto can be found, and vice versa. For details, see "Effective brackets string" section of the end of the title.

Nesting depth defined depth: i.e. the effective string parentheses nesting depth, depth (A) represents the effective depth of the nesting A string of brackets. For details, see the end of the title "nesting depth" section.

Nesting depth calculation method is effective with a corresponding bracket string type shown below:

1. empty string 2.AB type 3. (A) Type
"" (())() ((())())
depth=0 depth=max(2,1)=2 depth=1+depth(A)=3

 

To give you a "valid parenthesis string" seq, you divide it into two disjoint brackets valid strings, A and B, and the minimum depth of the two strings.

Disjoint: Each seq [i] can only be given to both A and B a, A can not both belong to B belongs.
A B the elements in the original string or may not be continuous.
A.length + B.length = seq.length
minimum depth: max (depth (A), depth (B)) is the smallest possible values. 
Partitioning scheme with a length of seq.length answer answer array, said encoding rules are as follows:

answer [i] = 0, seq [i] points to A.
answer [i] = 1, seq [i] points to B.
If there is more than meet the requirements of the answer, simply return any of which can be.

 

Example 1:

Input: seq = "(() ( ))"
Output: [0,1,1,1,1,0]
Example 2:

Input: seq = "() (( )) ()"
Output: [0,0,0,1,1,0,1,1]
Interpretation: This example is not the only answer.
Click to the output A = "() ()" , B = "() ()", max (depth (A), depth (B)) = 1, the minimum depth thereof.
As [1,1,1,0,0,1,1,1], the result is correct, wherein A = "() () ( )", B = "()", max (depth (A), depth (B)) = 1.
 

prompt:

1 < seq.size <= 10000
 

Effective brackets string:

Only by "(" and ")" character string composed of, for each of the left parenthesis, right parenthesis corresponding thereto can be found, and vice versa.
The following situations equally belongs to a valid string brackets:

1. empty string
2 is connected, can be written as AB (A is connected to B), wherein A and B are valid string brackets
3. nesting, can be written as (A), where A is a valid string brackets
fitted sets depth:

Similarly, we can define the effective depth of nesting brackets string s depth (S):

1. s is empty, depth ( "") = 0
when B 2. s is connected to the A, depth (A + B) = max (depth (A), depth (B)), where A and B are effective brackets string
3. s nested case, depth ( "(" + A + ")") = 1 + depth (A), where A is a valid string brackets

For example: "", "() ()" and "() (() ())" is a valid character string in brackets, 0,1,2 nesting depth respectively, while ") (" and "(( ) "is not a valid string brackets.

 

 

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {
        vector<int> ans;
        int height = 0;

        for(int i = 0; i < seq.length(); i++){
            if(seq[i] == '('){
                height+=1;
                ans.push_back(height%2);
            }
            else{
                ans.push_back(height%2);
                height--;
            }
        }

    return ans;
    }
};

Problem-solving ideas:

First, we must first think of matching parentheses stack, since parentheses match in line with the characteristics of LIFO stack, you see the last one left parenthesis is the first match on the right bracket will know.

Secondly, the string brackets are two types, one type is connected to a nested type, look at the subject to know, will produce only nested deeper nesting length.

So long as the nested structure open enough, the key is how the demolition.

Nesting depth is calculated as max (depth (A), depth (B))

So you either more, then the maximum depth will follow big. If and only if A, can reach a depth equal to the minimum value of B.

So we try to put the nested structure sharing chant, then according to the parity of points slightly

In fact, right parenthesis do not bother, go with his left parenthesis match, he also followed the go

In the inside of the stack, the stack height is exactly the depth of the string in brackets

Therefore, the depth of a left parenthesis give odd A, a depth of a left bracket to the B-even-like partial

Further notes that, in fact, only be used to maintain the stack height information, so we can simplify the height of the stack into a stack variable record, further compression space.

Guess you like

Origin www.cnblogs.com/olajennings/p/12616934.html