Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created October 28, 2018 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/bb1fc36f1ad4c2d9f19f100051da1bef to your computer and use it in GitHub Desktop.
Save fpdjsns/bb1fc36f1ad4c2d9f19f100051da1bef to your computer and use it in GitHub Desktop.
[leetcode] 921. Minimum Add to Make Parentheses Valid : https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
class Solution {
public:
int minAddToMakeValid(string S) {
int ans = 0;
stack<int> s;
for(int i=0;i<S.size();i++){
if(S[i] == '(')
s.push(i);
else if(s.empty())
ans++;
else
s.pop();
}
ans += s.size();
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment