首页 > 其他分享 >739.每日温度 daily-temperatures

739.每日温度 daily-temperatures

时间:2022-11-21 15:57:23浏览次数:67  
标签:res top 元素 daily st vector temperatures 739

问题描述

739.每日温度

解题思路

考虑利用单调栈(monotone stack)来进行处理,如果栈为空或者要入栈的元素小于栈顶元素,那么该元素入栈,否则弹出栈顶元素直到栈为空,或者要入栈的元素小于栈顶元素,再将该元素入栈。

这里应该将数组索引i入栈会比较方便。

代码

class Solution {
  public:
    vector<int> dailyTemperatures(vector<int> &temperatures) {
        vector<int> res(temperatures.size(), 0);
        stack<int> st;
        st.push(0);
        for (int i = 1; i < temperatures.size(); i++) {
            int j = i;
            // if (!st.empty()) {
            while (!st.empty() && temperatures[i] > temperatures[st.top()]) {
                res[st.top()] = i - st.top();
                st.pop();
            }
            st.push(i);
            // }
        }
        return res;
    }
};

标签:res,top,元素,daily,st,vector,temperatures,739
From: https://www.cnblogs.com/zwyyy456/p/16911620.html

相关文章

  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • 739 每日温度
    题目739每日温度给定一个整数数组temperatures,表示每天的温度,返回一个数组answer,其中answer[i]是指对于第i天,下一个更高温度出现在几天后。如果气温在这之后都......
  • Codeforces Round #739 (Div. 3) E
    E.PolycarpandStringTransformation显然我们可以通过看谁消失的最早知道删除序列然后有了删除序列以后我们能干什么呢显然对于每一个删除序列我们要是第一次就把......
  • 17.CF739C Alyona and towers 区间合并线段树
    17.CF739CAlyonaandtowers区间合并线段树给定序列,要求支持区间加,以及查询最长先增后减子区间(单峰序列)长度非常典型的区间合并线段树,记录左右起LIS,LCS,单峰洛谷传送门:......
  • Starting Daily apt upgrade and clean activities导致mysql自动重启
     早上收到预警,mysql发生了自动重启,查看mysql.log并没有发现异常,于是查看了一下/var/log/syslog,发现了以下日志 将自动更新服务停止sudoapt-getremoveunattended......
  • CF1739 部分题解
    比赛链接:https://codeforces.com/contest/1739AB水题//bySkyRainWind#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include<algo......
  • CF1739 C. Card Game
    题目链接题意简述有这样一个游戏,有一摞编号为\(1\simn\)的卡片(保证\(n\)为偶数),编号大的卡片比编号小的卡片更"强".两名玩家玩这个游戏,他们平均分这\(n\)......
  • cf1739D
    题意每次可以选择树上的一条边fa,u,将此边断开,连到根1上面询问k次操作后,一棵树的最小深度想法初见,第一反应直接贪心取最深的那一条路上的中间边经过几组思考,发现答案具......
  • LeetCode739 每日温度
    LeetCode739每日温度classSolution:defdailyTemperatures(self,temperatures:List[int])->List[int]:ans,stack,n=[0]*len(temperatures),[......
  • CF739C Alyona and towers 解题报告
    线段树绝世好题。题意:维护区间加,全局最长单峰序列。Solution:维护\(8\)个量。\(lval\):左端点的权值。\(rval\):右端点的权值。\(lmax\):以左端点开始的最长的下降序......