LeetCode: 1111. The nesting depth of effective parentheses "dichotomy" solution

Valid parenthesis strings consist only of "(" and ")" and meet one of the following conditions:

An empty string
concatenation, which can be recorded as AB (A and B are concatenated), where A and B are both valid parenthesis strings
Nested, which can be recorded as (A), where A is a valid parenthesis string

Similarly, we can define the nesting depth depth (S) of any valid bracket string s: when
s is empty, depth ("") = 0
s is when A and B are connected, depth (A + B) = max ( depth (A), depth (B)), where A and B are both valid parenthesis strings
s are nested, depth ("(" + A + ")") = 1 + depth (A), where A is Valid bracket string

For example: "", "() ()", and "() (() ())" are all valid parenthesis strings, the nesting depth is 0, 1, 2, respectively, and ") (" and "(( ) ”Are not valid parenthesis strings.

Give you a valid bracket string seq, divide it into two disjoint subsequences A and B, and A and B satisfy the definition of valid bracket string (note: A.length + B.length = seq.length).

Now, you need to select any set of valid bracket strings A and B to minimize the possible values ​​of max (depth (A), depth (B)).

Return the answer array answer whose length is seq.length. The coding rule for choosing A or B is: If seq [i] is part of A, then answer [i] = 0. Otherwise, answer [i] = 1. Even if there are multiple answers that meet the requirements, you only need to return one.

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

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

Tip:
1 <= text.size <= 10000

Source: LeetCode
Link: https://leetcode-cn.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Ideas

The title is very troublesome. This question means finding a way to divide the original string into two substrings, so that both substrings are valid parentheses, and the depth of the parentheses of the two substrings is the smallest. Today is fool day, so I thought at a glance This question is a 01 backpack, fooled

In fact, the idea is very simple: we want to reduce the depth of the string formed by the subsequences by dividing the two subsequences. This depth is actually the depth of the stack in valid brackets .

Reduce
the depth. The deepening caused by nesting cannot be avoided, but we can offset this depth by moving to a new sequence . For example, the current bracket is nested in a bracket with a depth of n, but as long as we select it to a subsequence B (assuming that B is empty at this time), then its depth is 0

However, as there are more and more parenthesis pairs selected in B, because the subsequence itself also has deep nesting, the depth of sequence B will become deeper and deeper. How to choose a selection strategy to balance the depth What?

Allocation strategy
Think of how balanced binary trees balance height? Try to divide the nodes equally on both sides

In the same way, we adopt the strategy of equal division, placing the brackets with odd depths in sequence A, and the brackets with even depths in sequence B.
Insert picture description here
Of course, there can be other strategies, for example, the first 1/2 of the depth is placed in A, and the last 1 / 2 Put it in B, as long as it is two points

Code

Note that there is no stack to simulate. Because the depth of the stack must be -1, and the depth of the stack must be +1, so you can maintain the int. Note that the depth is reduced after the stack is removed, so the depth- statement is placed after the judgment

class Solution {
public:
    vector<int> maxDepthAfterSplit(string seq)
    {
        vector<int> ans(seq.length());
        int depth = 0;
        for(int i=0; i<seq.length(); i++)
        {
            if(seq[i]=='(') depth++;

            if(depth%2==0) ans[i]=1;
            else ans[i]=0;

            if(seq[i]==')') depth--;
        }
        return ans;
    }
};
Published 262 original articles · won 11 · 10 thousand views

Guess you like

Origin blog.csdn.net/weixin_44176696/article/details/105238550