921. Minimum Add to Make Parentheses Valid (M)
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
Last updated
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
Last updated
int minAddToMakeValid(string s) {
// res 记录插入次数
int res = 0;
// need 变量记录右括号的需求量
int need = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
// 对右括号的需求 + 1
need++;
}
if (s[i] == ')') {
// 对右括号的需求 - 1
need--;
if (need == -1) {
need = 0;
// 需插入一个左括号
res++;
}
}
}
return res + need;
}