LeetCode:求每日的天气变暖的情况

博客围绕LeetCode,涉及算法与数据结构相关内容。在信息技术领域,算法和数据结构是重要基础,LeetCode是检验和提升相关能力的平台,能帮助开发者更好掌握这些知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a list of daily temperatures T,return a list such that,for each day in the input,tells
 you how many days you would have to wait until a warmer temperature.If there is no future 
 day for which this is possible,put 0 instead.
For example,given the list of temperatures T=[73,74,75,71,69,72,76,73],your output should 
be [1,1,4,2,1,1,0,0].
Note:
The length of temperatures will be in the range [1,30000].Each temperature will be an 
integer in the range[30,100].

Example 1:
Input:[73,74,75,71,69,72,76,73]
Output:[1,1,4,2,1,1,0,0]

题目大意:
给定每天的温度,求对于每一天需要等几天才可以等到更暖和的一天.如果该天之后不存在更暖和的天气,则记为0.

解题思路:
这道题比较理想的算法是利用单调栈,可以维持一个单调递减的栈,表示每天的温度:为了方便计算天数差,这里存放位置(即日期)
而非温度本身.从左向右遍历温度数组,对于每个日期p,如果p的温度比栈顶存储的位置q的温度高,则我们取出q,并且记录q需要
等待的天数p-q;我们重复这一过程,直到p的温度小于等于栈顶存储位置的温度(或空栈)时,将p插入栈顶,然后考虑下一天.
在这个过程中,栈内数组永远保持单调递减,避免了使用排序进行比较.最后栈内剩余一些日期,则说明它们之后都没有出现更
暖和的日期.
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

class Solution{
public:
    vector<int> dailyTemperature(vector<int>& temperatures){
        int n=temperatures.size();
        vector<int> ans(n);
        stack<int> index;
        for(int i=0;i<n;i++){
            while(!index.empty()){
                int pre_index=index.top();
                if(temperatures[i]<=temperatures[pre_index])
                    break;
                index.pop();
                ans[pre_index]=i-pre_index;
            }
            index.push(i);
        }
        return ans;
    }
};
int main(int argc,char* argv[]){
    vector<int> tempratures={73,74,75,71,69,72,76,73};
    vector<int> ans=Solution().dailyTemperature(tempratures);
    for_each(ans.begin(),ans.end(),[&](const int& num){
        cout<<num<<" ";
    });
    cout<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值