力扣:739. 每日温度

739. 每日温度

典型的单调栈问题,从右边遍历枚举维护单调栈即可。

答案存到数组ant中,

当前位置温度比栈顶大就栈顶--,退出循环再栈顶加入当前温度。

cpp 复制代码
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        
       const int N=temperatures.size();
        int stk[N+2];int tt=0;
        vector<int> ant(N);
        for(int i=N-1;i>=0;i--)
        {
            while(tt && temperatures[i]>=temperatures[stk[tt]])tt--;
            stk[++tt]=i;
            if(tt>1) ant[i]=stk[tt-1]-i;
            
        }
        return ant;
    }
};
相关推荐
报错小能手几秒前
C++异常处理 终极及总结
开发语言·c++
2501_941805313 分钟前
智慧零售平台中的多语言语法引擎与实时推荐系统实践
leetcode
Algo-hx7 分钟前
C++编程基础(九):预处理指令
c++
mit6.8246 小时前
bfs|栈
算法
tobebetter95276 小时前
How to manage python versions on windows
开发语言·windows·python
9***P3346 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger7 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz7 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
夏鹏今天学习了吗7 小时前
【LeetCode热题100(72/100)】前 K 个高频元素
leetcode
稚辉君.MCA_P8_Java7 小时前
DeepSeek 插入排序
linux·后端·算法·架构·排序算法