class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
for(int j=0,i=numbers.size()-1;j<numbers.size();j++)
{
while(i-1>j &&numbers[i-1]+numbers[j]>=target) i--;
if(numbers[i]+numbers[j]==target)
return {j+1,i+1};
}
return {-1,-1};
}
};
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int ed=m+n-1;
int p1=m-1,p2=n-1;
while(p1>=0 && p2>=0)
{
if(nums1[p1]>=nums2[p2])
nums1[ed--]=nums1[p1--];
else
nums1[ed--]=nums2[p2--];
}
while(p2>=0)
nums1[ed--]=nums2[p2--];
}
};
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty()) return 0;
int i=1;
for(int j=1;j<nums.size();j++)
{
if(nums[j]!=nums[j-1])
nums[i++]=nums[j];
}
return i;
}
};
class Solution {
public:
string minWindow(string s, string t) {
unordered_map<char,int>hash;
for(auto c:t)
hash[c]++;
int cnt=hash.size();
string res;
for(int i=0,j=0,c=0;i<s.size();i++)
{
if(hash[s[i]]==1) c++;
hash[s[i]]--;
while(hash[s[j]]<0) hash[s[j++]]++;
if(cnt==c)
{
if(res.empty() || res.size()>i-j+1) res=s.substr(j,i-j+1);
}
}
return res;
}
};
class Solution {
public:
int work(string s)
{
int res=0;
for(int i=0,start=0,cnt=0;i<s.size();i++)
{
if(s[i]=='(') cnt ++;
else
{
cnt--;
if(cnt<0) start=i+1,cnt=0;
if(cnt==0)
res=max(res,i-start+1);
}
}
return res;
}
int longestValidParentheses(string s) {
int res=work(s);
reverse(s.begin(),s.end());
for(auto&c:s) c ^=1;
return max(res,work(s));
}
};
class MinStack {
public:
/** initialize your data structure here. */
stack<int> stk,stk_min;
MinStack() {
}
void push(int x) {
stk.push(x);
if(stk_min.empty()) stk_min.push(x);
else stk_min.push(min(stk_min.top(),x));
}
void pop() {
stk.pop();
stk_min.pop();
}
int top() {
return stk.top();
}
int getMin() {
return stk_min.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
单调栈:查找每个数左侧第一个比它小的数
单调队列:滑动窗口中的最值
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n=heights.size();
vector<int> left(n),right(n);
stack<int>stk;
for(int i=0;i<n;i++)
{
while(stk.size() && heights[stk.top()]>=heights[i]) stk.pop();
if(stk.empty()) left[i]=-1;
else left[i]=stk.top();
stk.push(i);
}
while(stk.size()) stk.pop();
for(int i=n-1;i>=0;i--)
{
while(stk.size() && heights[stk.top()] >=heights[i]) stk.pop();
if(stk.empty()) right[i]=n;
else right[i]=stk.top();
stk.push(i);
}
int res=0;
for(int i=0;i<n;i++)
res=max(res,heights[i]*(right[i]-left[i]-1));
return res;
}
};
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> res;
deque<int> q;
for(int i=0;i<nums.size();i++)
{
if(q.size() && i-k+1>q.front()) q.pop_front();
while(q.size() && nums[q.back()] <= nums[i])
q.pop_back();
q.push_back(i);
if(i>=k-1) res.push_back(nums[q.front()]);
}
return res;
}
};
class Solution {
public:
int maxSubarraySumCircular(vector<int>& A) {
int n=A.size();
for(int i=0;i<n;i++) A.push_back(A[i]);
vector<int>sum(2*n+1);
for(int i=1;i<=n*2;i++)
sum[i]=sum[i-1]+A[i-1];
int res=INT_MIN;
deque<int>q;
q.push_back(0);
for(int i=1;i<=n*2;i++)
{
if(q.size() && i-n >q.front()) q.pop_front();
if(q.size()) res=max(res,sum[i]-sum[q.front()]);
while(q.size() && sum[q.back()] >=sum[i] ) q.pop_back();
q.push_back(i);
}
return res;
}
};