给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
暴力
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> ans(temperatures.size(),0);
for (int i = n - 2; i >= 0; i--) {
int j = i + 1;
while (j < n && temperatures[j] <= temperatures[i] && ans[j] != 0) {
j += ans[j];
}
if (j < n && temperatures[j] > temperatures[i]) {
ans[i] = j - i;
}
}
return ans;
}
};
单调栈
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> res(temperatures.size(),0);
stack<int> st;
st.push(0);
for(int i = 1; i < temperatures.size(); i++){
while(!st.empty() && temperatures[i] > temperatures[st.top()]){
res[st.top()] = i - st.top();
st.pop();
}
st.push(i);
}
return res;
}
};
nums1 中数字 x 的 下一个更大元素 是指 x 在 nums2 中对应位置 右侧 的 第一个 比 x 大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,下标从 0 开始计数,其中nums1 是 nums2 的子集。
对于每个 0 <= i < nums1.length ,找出满足 nums1[i] == nums2[j] 的下标 j ,并且在 nums2 确定 nums2[j] 的 下一个更大元素 。如果不存在下一个更大元素,那么本次查询的答案是 -1 。
返回一个长度为 nums1.length 的数组 ans 作为答案,满足 ans[i] 是如上所述的 下一个更大元素 。
暴力
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> res(nums1.size(),0);
for(int i = 0; i < nums1.size(); i++){
int j = 0;
while(j < nums2.size() && nums1[i] != nums2[j])
j++;
int k = j + 1;
while(k < nums2.size() && nums2[k] <= nums2[j])
k++;
if(k >= nums2.size())
res[i] = -1;
else
res[i] = nums2[k];
}
return res;
}
};
单调栈
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> res(nums1.size(),-1);
stack<int> st;
unordered_map<int,int> umap;
for(int i = 0; i < nums1.size(); i++)
umap[nums1[i]] = i;
st.push(0);
for(int i = 1; i < nums2.size(); i++){
while(!st.empty() && nums2[i] > nums2[st.top()]){
if(umap.find(nums2[st.top()]) != umap.end()){
res[umap[nums2[st.top()]]] = nums2[i];
}
st.pop();
}
st.push(i);
}
return res;
}
};
给定一个循环数组 nums ( nums[nums.length - 1] 的下一个元素是 nums[0] ),返回 nums 中每个元素的 下一个更大元素 。
数字 x 的 下一个更大的元素 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1 。
扩容
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
// 拼接一个新的nums
vector<int> nums1(nums.begin(), nums.end());
nums.insert(nums.end(), nums1.begin(), nums1.end());
vector<int> res(nums.size()*2,-1);
stack<int> st;
st.push(0);
for(int i = 1; i < nums.size(); i++){
while(!st.empty() && nums[i] > nums[st.top()]){
res[st.top()] = nums[i];
st.pop();
}
st.push(i);
}
res.resize(nums.size() / 2);
return res;
}
};
取模
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> res(nums.size(),-1);
stack<int> st;
st.push(0);
for(int i = 1; i < nums.size() * 2; i++){
while(!st.empty() && nums[i % nums.size()] > nums[st.top()]){
res[st.top()] = nums[i % nums.size()];
st.pop();
}
st.push(i % nums.size());
}
return res;
}
};
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
动态规划
class Solution {
public:
int trap(vector<int>& height) {
vector<int> maxLeft(height.size(), 0);
vector<int> maxRight(height.size(), 0);
int size = height.size();
// 记录每个柱子左边柱子最大高度
maxLeft[0] = height[0];
for (int i = 1; i < size; i++) {
maxLeft[i] = max(height[i], maxLeft[i - 1]);
}
// 记录每个柱子右边柱子最大高度
maxRight[size - 1] = height[size - 1];
for (int i = size - 2; i >= 0; i--) {
maxRight[i] = max(height[i], maxRight[i + 1]);
}
// 求和
int sum = 0;
for (int i = 0; i < size; i++) {
int count = min(maxLeft[i], maxRight[i]) - height[i];
if (count > 0) sum += count;
}
return sum;
}
};
单调栈
class Solution {
public:
int trap(vector<int>& height) {
int sum = 0;
stack<int> st;
st.push(0);
for(int i = 1; i < height.size(); i++){
while(!st.empty() && height[i] > height[st.top()]){
int right = height[i];
int low = st.top();
st.pop();
if(!st.empty()){
int left = height[st.top()];
sum += (min(left,right) - height[low]) * (i - st.top() - 1);
}
}
st.push(i);
}
return sum;
}
};
双指针
class Solution {
public:
int trap(vector<int>& height) {
int sum = 0;
int left = 0, right = height.size() - 1;
int leftMax = 0, rightMax = 0;
while (left < right) {
leftMax = max(leftMax, height[left]);
rightMax = max(rightMax, height[right]);
if (height[left] < height[right]) {
sum += leftMax - height[left];
++left;
} else {
sum += rightMax - height[right];
--right;
}
}
return sum;
}
};
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
双指针
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int n = heights.size();
vector<int> minLeftIndex(n);
vector<int> minRightIndex(n);
minLeftIndex[0] = -1;
for (int i = 1; i < n; i++) {
int t = i - 1;
while (t >= 0 && heights[t] >= heights[i]) t = minLeftIndex[t];
minLeftIndex[i] = t;
}
minRightIndex[n - 1] = n;
for (int i = n - 2; i >= 0; i--) {
int t = i + 1;
while (t < n && heights[t] >= heights[i]) t = minRightIndex[t];
minRightIndex[i] = t;
}
int res = 0;
for(int i = 0; i < n; i++){
int a = minRightIndex[i] - minLeftIndex[i] - 1;
res = max(res,a * heights[i]);
}
return res;
}
};
单调栈
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
heights.insert(heights.begin(),0);
heights.push_back(0);
stack<int> st;
st.push(0);
int res = INT_MIN;
for(int i = 1; i < heights.size(); i++){
while(!st.empty() && heights[i] < heights[st.top()]){
int h = heights[st.top()];
st.pop();
if(!st.empty()){
int left = st.top();
res = max(res,h * (i - left - 1));
}
}
st.push(i);
}
return res == INT_MIN ? 0 : res;
}
};