20221004 - 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加(贪心)

103 阅读2分钟

A parentheses string is valid if and only if:

  • It is the empty string,
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.

  • For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".

Return the minimum number of moves required to make s valid.

Example 1

Input: s = "())"
Output: 1

Example 2

Input: s = "((("
Output: 3

Constraints

  • 1 <= s.length <= 1000
  • s[i] is either '(' or ')'.

Solution

官方题解:

这道题是括号匹配的题目。每个左括号必须对应一个右括号,而且左括号必须在对应的右括号之前。

对于括号匹配的题目,常用的做法是使用栈进行匹配,栈具有后进先出的特点,因此可以保证右括号和最近的左括号进行匹配。其实,这道题可以使用计数代替栈,进行匹配时每次都取距离当前位置最近的括号,就可以确保平衡。

从左到右遍历字符串,在遍历过程中维护左括号的个数以及添加次数。

如果遇到左括号,则将左括号的个数加 1。

如果遇到右括号,则需要和前面的左括号进行匹配,具体做法如下:

如果左括号的个数大于 0,则前面有左括号可以匹配,因此将左括号的个数减 1,表示有一个左括号和当前右括号匹配;

如果左括号的个数等于 0,则前面没有左括号可以匹配,需要添加一个左括号才能匹配,因此将添加次数加 1。

遍历结束后,需要检查左括号的个数是否为 0。如果不为 0,则说明还有剩下的左括号没有匹配,对于每个剩下的左括号都需要添加一个右括号才能匹配,此时需要添加的右括号个数为剩下的左括号个数,将需要添加的右括号个数加到添加次数。

无论是哪种添加的情况,都是在遇到括号无法进行匹配的情况下才进行添加,因此上述做法得到的添加次数是最少的。

int minAddToMakeValid(char * s){
    int i = 0, cnt = 0, add = 0;
    while (s[i]) {
        while (s[i] == '(') {
            cnt++;
            i++;
        }
        while (s[i] == ')') {
            if (cnt == 0) add++;
            else cnt--;
            i++;
        }
    }
    return add + cnt;
}

题目链接:921. 使括号有效的最少添加 - 力扣(LeetCode)