This is one of those problems where the clean idea matters more than the amount of code. For 1249. Minimum Remove to Make Valid Parentheses, the solution in this repository is mainly a stack solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minRemoveToMakeValid.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 60 ms, faster than 11.39% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
02//Memory Usage: 11.1 MB, less than 100.00% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
03class Solution {
04public:
05 string minRemoveToMakeValid(string s) {
06 int i = 0;
07 int open = 0, close = 0;
08
09 //delete redundant )
10 while(i < s.size()){
11 // cout << i << " " << open << " " << close << endl;
12 if(s[i] == '('){
13 open++;
14 }else if(s[i] == ')'){
15 close++;
16 if(close > open){
17 //if we don't add 2nd argument, it will delete to the end!
18 s.erase(i, 1);
19 close--;
20 i--;
21 }
22 }
23 i++;
24 }
25 // cout << s << endl;
26
27 //delete redundant (
28 i = s.size()-1;
29 open = 0;
30 close = 0;
31 while(i >= 0){
32 // cout << i << " " << open << " " << close << endl;
33 if(s[i] == '('){
34 open++;
35 if(open > close){
36 s.erase(i, 1);
37 open--;
38 //if traverse in reverse order, don't need this!
39 // i++;
40 }
41 }else if(s[i] == ')'){
42 close++;
43 }
44 i--;
45 }
46
47 return s;
48 }
49};
50
51//Stack
52//https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/discuss/419402/JavaC%2B%2B-Stack
53//Runtime: 64 ms, faster than 9.24% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
54//Memory Usage: 12.6 MB, less than 100.00% of C++ online submissions for Minimum Remove to Make Valid Parentheses.
55class Solution {
56public:
57 string minRemoveToMakeValid(string s) {
58 stack<int> stk;
59 for(int i = 0; i < s.size(); i++){
60 if(s[i] == '('){
61 //convert to 1-based so that we can use '-' as mark for
62 stk.push(i+1);
63 }else if(s[i] == ')'){
64 //stk's top is '('
65 if(stk.size() > 0 && stk.top() > 0) stk.pop();
66 else stk.push(-(i+1));
67 }
68 }
69
70 //now stk contains redundant char's indices
71 while(!stk.empty()){
72 int i = stk.top(); stk.pop();
73 s.erase(abs(i)-1, 1);
74 }
75
76 return s;
77 }
78};
Cost