LeetCode打卡 day58--单调栈


知识总结

单调栈的应用, 就是需要构建一个单调递增或者单调递减的栈, 去解决下一个大(小)的元素的问题


Leetcode 739. 每日温度

题目链接

题目说明

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替

在这里插入图片描述

代码说明

用一个单调递增的stack来储存遍历的温度, 当栈顶的值小于当前的遍历的温度的时候, 说明栈顶的温度已经找到了下一个温度更高的天气, 弹栈.
留在栈的元素说明还没有碰到气温比自己更高的

因为需要记录间隔的天数, 所有栈里面存的是index而不是数值

class Solution {
    
    
    public int[] dailyTemperatures(int[] temperatures) {
    
    
        //单调栈, 存放的数据如果比栈顶元素小, 直接存
        //如果比栈顶元素大,将小的元素弹出来, 并且记录下结果
        int[] res = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < temperatures.length; i++){
    
    
            if(stack.isEmpty()){
    
    
                stack.push(i);
                continue;
            }
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
    
    
                int index = stack.pop();
                res[index] = i - index;
            }
            stack.push(i);
        }
        return res;
    }
}

Leetcode 496. 下一个更大元素 I

题目链接

题目说明

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] 是如上所述的 下一个更大元素

在这里插入图片描述

代码说明

继续用单调栈, 但是因为nums1和nums2 位置对应不上, 所以需要一个额外的hasmmap来记录元素对应的index

class Solution {
    
    
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
    
    
        int len1 = nums1.length, len2 = nums2.length;
        Stack<Integer> stack = new Stack<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] res= new int[len1];
        Arrays.fill(res, -1);
        for(int i = 0; i < len1;i++){
    
    
            map.put(nums1[i], i);
        }
        stack.push(nums2[0]);
        for(int i = 1; i < len2; i++){
    
    
            while(!stack.isEmpty() && nums2[i] > stack.peek()){
    
    
                int val = stack.pop();
                if(map.containsKey(val)){
    
    
                    res[map.get(val)] = nums2[i];
                }
            }
            stack.push(nums2[i]);
        } 
        return res;
    }
}

当然也可以直接暴力求解

class Solution {
    
    
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
    
    
        // Stack<Integer> stack = new Stack<>();
        int[] res= new int[nums1.length];
        Arrays.fill(res, -1);
        boolean lastSeen = false;
        for(int i = 0; i < nums1.length; i++){
    
    
            lastSeen = false;
            for(int j = 0; j < nums2.length; j++){
    
    
                if(nums1[i] == nums2[j]){
    
    
                    lastSeen = true;
                    continue;
                }
                if(lastSeen && nums2[j] > nums1[i]){
    
    
                    res[i] = nums2[j];
                    break;
                }
            }
        }
        return res;

        
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45872648/article/details/131585289
今日推荐