文章目录
problem Ⅰ
1249. Minimum Remove to Make Valid Parentheses
Given a string s of ‘(’ , ‘)’ and lowercase English characters.
Your task is to remove the minimum number of parentheses ( ‘(’ or ‘)’, in any positions ) so that the resulting parentheses string is valid and return any valid string.
Formally, a parentheses string is valid if and only if:
It is the empty string, contains only lowercase characters, or
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.
Example 1:
Example 2:
Example 3:
my solution 1 stack
class Solution {
public:
string minRemoveToMakeValid(string s) {
stack<int> stk;
for(int i=0; i<s.size(); i++){
if(s[i]=='(')
stk.push(i);
else if(s[i]==')'){
if(!stk.empty())
stk.pop();
else
s[i]='#';
}
}
while(!stk.empty()){
s[stk.top()]='#';
stk.pop();
}
string ans = "";
for(int i=0; i<s.size(); i++){
if(s[i] != '#')
ans.push_back(s[i]);
}
return ans;
}
};
my solution 2 cnt
class Solution {
public:
string minRemoveToMakeValid(string s) {
int cnt = 0;
// from left to right
for(int i=0; i<s.size(); i++){
if(s[i] == '(') cnt++;
else if(s[i] == ')'){
if(cnt == 0) s[i] = '#';
else cnt--;
}
}
// from right to left
cnt = 0;
for(int i=s.size(); i>=0; i--){
if(s[i] == ')') cnt++;
else if(s[i] == '('){
if(cnt == 0) s[i] = '#';
else cnt--;
}
}
//return ans
string ans = "";
for(int i=0; i<s.size(); i++){
if(s[i] != '#')
ans.push_back(s[i]);
}
return ans;
}
};
problem Ⅱ
my solution wrong
class Solution {
public:
int findTheWinner(int n, int k) {
vector<int> que(n, 0);
int idx = 0;
for(int i=0; i<n-1; i++){// n loop
int step = 0;
while(step < k-1){
if(que[idx]==0)
step++;
idx = (idx+1)%n;
}
que[idx]=1;
idx = (idx+1)%n;
}
for(int i=0; i<n; i++){
if(que[i]==0)return i+1;
}
return -1;
}
};
my solution queue
class Solution {
public:
int findTheWinner(int n, int k) {
queue<int> que;
for(int i=1; i<=n; i++)
que.push(i);
while(que.size() != 1){
for(int i=0; i<k-1; i++){
int tmp = que.front();
que.pop();
que.push(tmp);
}
que.pop();
}
return que.front();
}
};
time complexity
:
O
(
n
)
+
O
(
(
n
−
1
)
∗
(
k
−
1
)
)
=
O
(
n
∗
k
)
O(n)+O((n-1)*(k-1))=O(n*k)
O(n)+O((n−1)∗(k−1))=O(n∗k)
space complexity
:
O
(
n
)
O(n)
O(n)
problem Ⅲ
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
- MinStack() initializes the stack object.
- void push(int val) pushes the element val onto the stack.
- void pop() removes the element on the top of the stack.
- int top() gets the top element of the stack.
- int getMin() retrieves the minimum element in the stack.
Example 1:
Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output
[null,null,null,null,-3,null,0,-2]
Explanation
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
solution
class MinStack {
public:
vector< pair<int,int> > s;
MinStack() { }
void push(int val) {
if(s.empty())
s.push_back({val,val});
else
s.push_back({val,min(s.back().second,val)});
}
void pop() { s.pop_back(); }
int top() { return s.back().first; }
int getMin() { return s.back().second; }
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/