力扣爆刷第134天之单调栈五连刷(经典接雨水)
总结:
如果要求某一个数左边或者右边第一个比他大或者比他小的数,这种题就是典型的单调栈解法。
一、739. 每日温度
题目链接:https://leetcode.cn/problems/daily-temperatures/description/
思路:求每天以后最近的大于它的一天,使用单调栈即可,既然是求第一个大于第i天温度的天数,自然是当前元素大于栈顶元素时,出栈计算距离。
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
LinkedList<Integer> stack = new LinkedList<>();
int[] answer = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++) {
while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
int index = stack.poll();
answer[index] = i - index;
}
stack.push(i);
}
return answer;
}
}
二、496. 下一个更大元素 I
题目链接:https://leetcode.cn/problems/next-greater-element-i/description/
思路:本题有两个数组,多了一层映射关系,可以使用一个map记录第一个数组的值和索引,然后就用单调栈处理第二个数组,当前元素大于栈顶元素时,栈顶出栈,看看是否在第一个数组里,就可以记录下第一个最近的大元素。
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2){
int[] result = new int[nums1.length];
Arrays.fill(result, -1);
LinkedList<Integer> stack = new LinkedList<>();
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums1.length; i++) {
map.put(nums1[i], i);
}
for(int i = 0; i < nums2.length; i++) {
while(!stack.isEmpty() && nums2[i] > nums2[stack.peek()]) {
int key = nums2[stack.poll()];
if(map.containsKey(key)) {
result[map.get(key)] = nums2[i];
}
}
stack.push(i);
}
return result;
}
}
三、503. 下一个更大元素 II
题目链接:https://leetcode.cn/problems/next-greater-element-ii/description/
思路:本题相比于一般的求下一个更大元素的题,多了要给要求,就是视数组为循环数组,即首尾相接,来求下一个更大元素,解法很简单,就是遍历两边数组即可。把数组的长度虚拟延长到原本两倍即可。
class Solution {
public int[] nextGreaterElements(int[] nums) {
int[] result = new int[nums.length];
Arrays.fill(result, -1);
LinkedList<Integer> stack = new LinkedList<>();
for(int i = 0; i < nums.length * 2; i++) {
int j = i % nums.length;
while(!stack.isEmpty() && nums[j] > nums[stack.peek()]) {
int index = stack.poll();
result[index] = nums[j];
}
stack.push(j);
}
return result;
}
}
四、42. 接雨水
题目链接:https://leetcode.cn/problems/trapping-rain-water/description/
思路:接雨水是典型的单调栈题目,只需要构造一个凹槽就可以解决。直接栈内,自栈底到栈顶单调递减,遇到大于栈顶的元素,就是直接让栈顶出栈,此时出栈的就是凹槽底部,这样就可以一个是计算宽度,另一个就是计算深度,然后累计即可。
class Solution {
public int trap(int[] height) {
LinkedList<Integer> stack = new LinkedList<>();
int sum = 0;
for(int i = 0; i < height.length; i++) {
while(!stack.isEmpty() && height[i] > height[stack.peek()]) {
int top = stack.poll();
if(!stack.isEmpty()) {
int w = i - stack.peek() - 1;
int h = Math.min(height[i], height[stack.peek()]) - height[top];
sum += w*h;
}
}
stack.push(i);
}
return sum;
}
}
五、84. 柱状图中最大的矩形
题目链接:https://leetcode.cn/problems/largest-rectangle-in-histogram/description/
思路:本题正好和上一题反过来了,上一题求的是凹槽,本题是寻找凸起,从栈底到栈顶是单调递增,当前元素小于栈顶元素时,出栈,此时出栈的元素即为凸出处,即可可以计算宽度和高度,然后计算面积。
class Solution {
public int largestRectangleArea(int[] heights) {
LinkedList<Integer> stack = new LinkedList<>();
int[] heightsNew = new int[heights.length+2];
for(int i = 0; i < heights.length; i++) {
heightsNew[i+1] = heights[i];
}
heights = heightsNew;
int max = 0;
for(int i = 0; i < heights.length; i++) {
while(!stack.isEmpty() && heights[i] < heights[stack.peek()]) {
int mid = stack.poll();
if(!stack.isEmpty()) {
int w = i - stack.peek() - 1;
int h = heights[mid];
max = Math.max(max, w*h);
}
}
stack.push(i);
}
return max;
}
}