力扣: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;
    }
};
相关推荐
天远云服31 分钟前
天远企业司法认证API对接实战:PHP构建B2B供应链合规防火墙
大数据·开发语言·后端·node.js·php
空空kkk38 分钟前
Java基础——代理
java·开发语言
赵谨言39 分钟前
基于YOLOv5的植物目标检测研究
大数据·开发语言·经验分享·python
野生技术架构师39 分钟前
互联网大厂必备 Java 面试八股文真题解析
java·开发语言·面试
不光头强1 小时前
IO流知识点
开发语言·python
老约家的可汗1 小时前
C++篇之类和对象下
java·开发语言·c++
Mr_WangAndy1 小时前
C++数据结构与算法_排序算法
c++·排序算法·基础排序·高级排序
水月wwww1 小时前
Rust的安装与卸载 | windows
开发语言·windows·rust
niuniudengdeng1 小时前
六面独立转动魔方还原机器人设计与实现
数学·算法·机器人
ghie90901 小时前
基于MATLAB的A*算法避障路径规划实现
人工智能·算法·matlab