739. 每日温度
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> res;
int stk[temperatures.size()+1],tt=0;
for(int i=temperatures.size()-1;i>=0;i--){
while(tt&&temperatures[stk[tt]]<=temperatures[i]) tt--;
if(tt) res.push_back(stk[tt]);
else res.push_back(0);
stk[++tt]=i;
}
reverse(res.begin(),res.end());
for(int i=0;i<temperatures.size();i++) if(res[i]!=0) res[i]=res[i]-i;
return res;
}
496. 下一个更大元素 I
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int,int> map;
int stk[nums2.size()+1],tt=0;
vector<int> res;
for(int i=nums2.size()-1;i>=0;i--){
while(tt&&stk[tt]<=nums2[i]) tt--;
if(tt) map[nums2[i]]=stk[tt];
else map[nums2[i]]=-1;
stk[++tt]=nums2[i];
}
for(int i=0;i<nums1.size();i++) res.push_back(map[nums1[i]]);
return res;
}
503. 下一个更大元素 II
vector<int> nextGreaterElements(vector<int>& nums) {
int n=nums.size();
vector<int> res;
int stk[n+1],tt=0;
for(int i=0;i<n;i++) nums.push_back(nums[i]);
for(int i=nums.size()-1;i>=0;i--){
while(tt&&stk[tt]<=nums[i]) tt--;
if(tt) res.push_back(stk[tt]);
else res.push_back(-1);
stk[++tt]=nums[i];
}
reverse(res.begin(),res.end());
return vector<int>(res.begin(),res.begin()+n);
}
42. 接雨水
int trap(vector<int>& height) {
int res=0;
vector<int>lheight(height.size(),0);
vector<int>rheight(height.size(),0);
lheight[0]=height[0],rheight[height.size()-1]=height[height.size()-1];
for(int i=1;i<height.size();i++){
lheight[i]=max(height[i],lheight[i-1]);
}
for(int i=height.size()-2;i>=0;i--){
rheight[i]=max(height[i],rheight[i+1]);
}
for(int i=0;i<height.size();i++){
res+=min(rheight[i],lheight[i])-height[i];
}
return res;
}
84. 柱状图中最大的矩形
int largestRectangleArea(vector<int>& heights) {
int res=0;
vector<int> lheight(heights.size()+1,0);
vector<int> rheight(heights.size()+1,0);
lheight[0]=-1;rheight[heights.size()-1]=heights.size();
for(int i=1;i<heights.size();i++){
int t=i-1;
while(t>=0&&heights[t]>=heights[i]) t=lheight[t];
lheight[i]=t;
}
for(int i=heights.size()-2;i>=0;i--){
int t=i+1;
while(t<heights.size()&&heights[t]>=heights[i]) t=rheight[t];
rheight[i]=t;
}
for(int i=0;i<heights.size();i++){
int s=heights[i]*(rheight[i]-lheight[i]-1);
res=max(res,s);
}
return res;
}