代码随想录算法训练营第58天 | 739、496

文章介绍了如何使用单调栈解决两个编程问题:一是给定温度数组,找到每个温度后更高的温度需要等待的天数;二是找到一个数组中每个元素在另一个大数组中的下一个更大元素。两种问题都涉及到了单调栈的数据结构和遍历技巧,解法包括两种不同的实现方式。

739. 每日温度

题目描述

给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
示例1:
输入:temperatures=[73,74,75,71,69,72,76,73]temperatures = [73,74,75,71,69,72,76,73]temperatures=[73,74,75,71,69,72,76,73]
输出:[1,1,4,2,1,1,0,0][1,1,4,2,1,1,0,0][1,1,4,2,1,1,0,0]
示例2:
输入:temperatures=[30,40,50,60]temperatures = [30,40,50,60]temperatures=[30,40,50,60]
输出:[1,1,1,0][1,1,1,0][1,1,1,0]
示例3:
输入:temperatures=[30,60,90]temperatures = [30,60,90]temperatures=[30,60,90]
输出:[1,1,0][1,1,0][1,1,0]

思路

单调栈的应用典中典
主要需要在意的是三个判断条件
1)当前遍历的元素小于栈顶元素
2)当前遍历的元素等于栈顶元素
3)当前遍历的元素大于栈顶元素
一般情况下2)会与1)或3)相结合一同判断

解法1

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int lens = temperatures.length;
        int[] res = new int[lens];
        Stack<Integer> stk = new Stack<>();
        stk.push(0);
        for(int i = 1;i < lens;i++){
            if(temperatures[i] <= temperatures[stk.peek()]){
                stk.push(i);
            }
            else{
                while(!stk.isEmpty() && temperatures[i] > temperatures[stk.peek()]){
                    res[stk.peek()] = i - stk.peek();
                    stk.pop();
                }
                stk.push(i);
            }
        }
        return res;
    }
}

解法2

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int lens = temperatures.length;
        int[] res = new int[lens];
        Stack<Integer> stk = new Stack<>();
        for(int i = 0;i<lens;i++){
            while(!stk.isEmpty() && temperatures[i] > temperatures[stk.peek()]){
                res[stk.peek()] = i - stk.peek();
                stk.pop();
            }
            stk.push(i);
        }
        return res;
    }
}

总结

单调栈还是第一次学,从本题上看还是比较好识别的。好好看,好好学。

496. 下一个更大元素Ⅰ

题目描述

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] 是如上所述的 下一个更大元素 。
示例1:
输入:nums1=[4,1,2],nums2=[1,3,4,2]nums1 = [4,1,2], nums2 = [1,3,4,2]nums1=[4,1,2],nums2=[1,3,4,2]
输出:[−1,3,−1][-1,3,-1][1,3,1]
示例2:
输入:nums1=[2,4],nums2=[1,2,3,4]nums1 = [2,4], nums2 = [1,2,3,4]nums1=[2,4],nums2=[1,2,3,4]
输出:[3,−1][3,-1][3,1]

思路

本题与上一题类似,但有一个需要注意的点是,要判断nums2此时的元素是否在nums1中出现过,只有出现过才能记录,否则要跳过。

解法1

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

解法2

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

总结

相当于上一题的升级版,也是对单调栈应用的考察,好好看,好好学。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值