Created
October 28, 2018 02:55
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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