当前位置:网站首页>单调栈——739. 每日温度
单调栈——739. 每日温度
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/daily-temperatures
2 题目示例
示例 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]
3 题目提示
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100
4 思路
可以维护一个存储下标的单调栈,从栈底到栈顶的下标对应的温度列表中的温度依次递减。如果一个下标在单调栈里,则表示尚未找到下一次温度更高的下标。
正向遍历温度列表。对于温度列表中的每个元素temperatures[i] ,如果栈为空,则直接将i进栈,如果栈不为空,则比较栈顶元素prevIndex对应的温度temperatures[prevIndex]和当前温度temperatures[i],如果termperatures[i] > temperaturesLprevIndex] ,则将prevIndex移除,并将prevIndex对应的等待天数赋为i -prevIndex ,重复上述操作直到栈为空或者栈顶元素对应的温度小于等于当前温度,然后将i 进栈。
为什么可以在弹栈的时候更新ans[prevIndex]呢?因为在这种情况下,即将进栈的i对应的temperatures[i]一定是temperatures[prevIndex]右边第一个比它大的元素,试想如果prevIndex和i有比它大的元素,假设下标为j,那么prevIndex一定会在下标j的那—轮被弹掉。
由于单调栈满足从栈底到栈顶元素对应的温度递减,因此每次有元素进栈时,会将温度更低的元素全部移除,并更新出栈元素对应的等待天数,这样可以确保等待天数一定是最小的。
以下用一个具体的例子帮助读者理解单调栈。对于温度列表[73,74,75,71,69,72,76,73],单调栈stack的初始状态为空,答案ans的初始状态是[0,0,0,0,0,0,0,0],按照以下步骤更新单调栈和答案,其中单调栈内的元素都是下标,括号内的数字表示下标在温度列表中对应的温度。

复杂度分析
时间复杂度:O(n),其中n是温度列表的长度。正向遍历温度列表—遍,对于温度列表中的每个下标,最多有一次进栈和出栈的操作。
空间复杂度:O(n),其中n是温度列表的长度。需要维护一个单调栈存储温度列表中的下标。
5 我的答案
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int length = temperatures.length;
int[] ans = new int[length];
Deque<Integer> stack = new LinkedList<Integer>();
for (int i = 0; i < length; i++) {
int temperature = temperatures[i];
while (!stack.isEmpty() && temperature > temperatures[stack.peek()]) {
int prevIndex = stack.pop();
ans[prevIndex] = i - prevIndex;
}
stack.push(i);
}
return ans;
}
}
边栏推荐
- IO analog serial port of stm32
- Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
- 嵌入式数据库--SQLite
- Leaf recognition, color feature extraction, defect detection, etc
- 4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
- Practice of online problem feedback module (16): realize the function of checking details
- D2dengine edible tutorial (4) -- draw text
- Unity简单实现对话功能
- How to make the Internet access the intranet IP (used by esp8266 web pages)
- 超好看的Nteam官网PHP程序源码
猜你喜欢
随机推荐
[5g NR] RRC reject analysis
[download file] uniapp develops small programs, downloads files and saves them locally
Response to questions about the balanced beacon group of Hubei University of Arts and Sciences
Contour detection based on OpenCV (3)
如何使用JDBC操作数据库
Shell:资源监控脚本和高负载报警
Four methods of closing forms in C #
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
Redis basic operation
SSM integration (integrated configuration)
Redis memory recycling
Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
Summary of redis classic interview questions
Practice of online problem feedback module (16): realize the function of checking details
20220726汇承科技的蓝牙模块HC-05的AT命令测试
Animation
20条心灵鸡汤唯美句子,句句温情暖心!
Unity simply implements the dialog function
ES6 从入门到精通 # 09:Symbol 类型
Methods of SQL server backup database








