LeetCode856 Score of Parentheses

this is a problem related to mono stack.
and the problem is pretty simple:
Given a balanced parentheses string S, compute the score of the string based on the following rule:

  1. () has score 1
  2. AB has score A + B, where A and B are balanced parentheses strings.
  3. (A) has score 2 * A, where A is a balanced parentheses string.

so in one sentence: calculate the points by given rules.
example:
Input: “(()(()))”
Output: 6

so it’s like, count the number of complete parentheses. (but will the input contains invalid partentheses?)

as we already known, many parentheses problem can be solved in stack.
so instead of check the given string is valid parentheses, we count the number everytime we find a valid parenthese, but pay attention of rule3: (A) has score 2 * A, where A is a balanced parentheses string.

what does the meaning of rule3? it means for every level we go down, we (double starting 1)+(the score we already got from other paralle parentheses)

//cur record the score at the current layer level.
    public int scoreOfParentheses(String S) {
        Stack<Integer> stack = new Stack<>();
        int cur = 0;
        for (char c : S.toCharArray()) {
            if (c == '(') {
                stack.push(cur);
                cur = 0;
            } else {
                cur = stack.pop() + Math.max(cur * 2, 1);
            }
        }
        return cur;
    }

so the general idea is:

If we meet ‘(’,
we push the current score to stack,
enter the next inner layer level,
and reset cur = 0.

If we meet ‘)’,
the cur score will be doubled and will be at least 1.
We exit the current layer level,
and set cur = stack.pop() + cur.

if you still find the aboved explaination is hard to understand, think of in this instance: the instance with “((()))”, so we pushed three 0s in the stack, and cur still be 0 after the last push. and now, each time we pop, the value poped out is always 0. and cur will be 1, 2, and 4. which is exactly the right results for this answer.
another instance: (()()), so this invloves parallel and series(并联与串联) of parentheses. so we pushed two 0s in stack, and cur keeps as 0; and then, we met our first ), so cur becomes 1 and stack only left one 0s. and then, we met the ‘(’ again, so we push 1 into stack, and reset cur as 0. and then, there only left two ')'s, so when cur = 1+1 = 2, and cur = 0+2*2 = 4. so that the answer and that’s obviously correct.
so we can see from those 2 instances: the size of stack is actually the level we currently at, and the value of each level in that stack represents for the parallel thread of current level. if the value is 0, then means we have only one thread in current level, if the value is 1, then we have 2 threads… and now we know how this solution is build and why it is correct.

follow up:
can we solve in O(1) space?

think about this, why do we need to use stack? because we need to keep record of what level we are in and what paralle parentheses we at this level.

so the code can be written as:

    public int scoreOfParentheses(String S) {
        int res = 0, l = 0;
        for (int i = 0; i < S.length(); ++i) {
            if (S.charAt(i) == '(') {
                l++; 
            } else { //this means when s.charAt(i) == ')', this statement will be executed and so is the next if
                l--;
            if (S.charAt(i) == ')' && S.charAt(i - 1) == '(') { //if we have a adjacent pair ()
                res += 1 << l; //what is the order of execution of << and +
            }
        }
        return res;
    }

not fully understand the code, but get the general idea.

posted @ 2020-05-26 09:22  EvanMeetTheWorld  阅读(27)  评论(0)    收藏  举报