Nesting depth of a valid title 1111. parentheses LeetCode daily

Nesting depth of a valid title 1111. parentheses LeetCode daily

2020.4.1 medium difficulty
effectively defined string in parentheses: for each left parenthesis, right parenthesis can be found in the corresponding 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.

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.
+ = Seq.length B.length a.length
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]

prompt:

1 <= text.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. Null string
  2. Connection may be referred to as AB (A and B are connected), wherein A and B are valid string brackets
  3. Nesting, can be written as (A), where A is a valid string brackets
    nesting depth:

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

  1. s is empty, depth ( "") = 0
  2. When B is connected to the s A, depth (A + B) = max (depth (A), depth (B)), where A and B are valid string brackets
  3. s is 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.

answer

Here we will make a valid string is divided into two brackets, and to ensure a minimum depth of nesting, then we we need to know the depth of nesting, and then he points respectively to the two sub-strings to ensure his minimum depth . The answer here is not unique, as long as the minimum depth can be. Here we do not even need to actually maintain the stack, because we only need to know his depth.

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq) {
        vector<int> ans;
        int depth = 0;
        int length = seq.length();
        for(int i = 0;i < length; i++){
            if(seq[i] == ')' && depth >= 1){
                depth--;
            }
            ans.push_back(depth % 2);
            if(seq[i] == '('){
                depth++;
            }
        }
        return ans;
    }
};
Published 161 original articles · won praise 400 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43422111/article/details/105243718