【LeetCode热题100】739. 每日温度(栈)

一.题目要求

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

二.题目难度

中等

三.输入样例

示例 1:
输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]

示例 2:
输入: temperatures = [30,40,50,60]
输出: [1,1,1,0]

示例 3:
输入: temperatures = [30,60,90]
输出: [1,1,0]

提示:
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100

四.解题思路

解法1:存每个数的下标,但思路太复杂,让gpt帮我记录一下:
在这里插入图片描述
解法2:单调栈,也就是我上面做法的优化

在这里插入图片描述

五.代码实现

单调栈

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n = temperatures.size();
        stack<int> stk;
        vector<int> ans(n);
        for(int i = 0; i < n; i++)
        {
            if(stk.empty())
            {
                stk.push(i);
            }
            else
            {
                while(!stk.empty() && temperatures[i] > temperatures[stk.top()])
                {
                    ans[stk.top()] = i - stk.top();
                    stk.pop();
                }
                stk.push(i);
            }
        }
        return ans;
    }
};

自己的解法,仅记录

class Solution {
public:

    static int cmp(pair<int,int> a, pair<int,int> b)
    {
        return a.first < b.first;
    }

    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> stk;
        stack<pair<int, int>> count;
        vector<pair<int, int>> ans;
        for(int i = 0; i < temperatures.size(); i++) 
        {
            if(stk.empty()) 
            {
                stk.push(temperatures[i]);
                count.push({i, 0});
            }
            else 
            {
                while(!stk.empty() && temperatures[i] > stk.top()) 
                {
                    
                    ans.push_back({count.top().first, count.top().second + 1});
                    int tmp = count.top().second;
                    count.pop();
                    if(!count.empty())
                        count.top().second =count.top().second + tmp + 1;
                    stk.pop();
                }
                stk.push(temperatures[i]);
                count.push({i, 0});
            }
        }
        while(!count.empty()) {
             ans.push_back({count.top().first, 0});
             count.pop();
        }
        sort(ans.begin(), ans.end(), cmp);

        vector<int> aans; for(auto v : ans) aans.push_back(v.second);

       return aans;
    }
};

六.题目总结

判别是否需要使用单调栈,如果需要找到左边或者右边第一个比当前位置的数大或者小,则可以考虑使用单调栈。

相关推荐

  1. LeetCode100】【每日温度

    2024-04-01 17:50:03       17 阅读
  2. LeetCode-739. 每日温度 数组 单调

    2024-04-01 17:50:03       42 阅读
  3. LeetCode739】每日温度

    2024-04-01 17:50:03       37 阅读
  4. Leetcode 739. 每日温度

    2024-04-01 17:50:03       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 17:50:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 17:50:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 17:50:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 17:50:03       20 阅读

热门阅读

  1. redis分布式锁-----基于redisson实现分布式锁

    2024-04-01 17:50:03       18 阅读
  2. Vue的生命周期总结

    2024-04-01 17:50:03       20 阅读
  3. 单例设计模式(1)

    2024-04-01 17:50:03       19 阅读
  4. 第十四届省赛大学B组(C/C++)接龙数列

    2024-04-01 17:50:03       21 阅读
  5. bash工具-dir_util.sh

    2024-04-01 17:50:03       21 阅读
  6. python 三层架构思想写代码。

    2024-04-01 17:50:03       19 阅读
  7. python 移位运算符

    2024-04-01 17:50:03       20 阅读
  8. TTL值(Time-To-Live)简介

    2024-04-01 17:50:03       17 阅读
  9. NoSQL(非关系型数据库)之Redis

    2024-04-01 17:50:03       37 阅读
  10. 编程练习(python)

    2024-04-01 17:50:03       15 阅读
  11. 大模型之路1:趟一条小路

    2024-04-01 17:50:03       20 阅读
  12. 关于python中常用命令(持续更新中)

    2024-04-01 17:50:03       23 阅读