forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
39 lines (30 loc) · 733 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// Source : https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
/// Author : liuyubobobo
/// Time : 2018-10-13
#include <iostream>
#include <stack>
using namespace std;
/// Using Stack
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int minAddToMakeValid(string S) {
stack<char> s;
for(char c: S)
if(c == '(')
s.push(c);
else{
if(!s.empty() && s.top() == '(')
s.pop();
else
s.push(c);
}
return s.size();
}
};
int main() {
cout << Solution().minAddToMakeValid("()))((") << endl;
// 4
return 0;
}