当前位置:网站首页>Monotonic stack - 739. Daily temperature
Monotonic stack - 739. Daily temperature
2022-07-28 03:45:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Given an array of integers temperatures , Indicates the daily temperature , Returns an array answer , among answer[i] Refers to the i God , The next higher temperature appears in a few days . If the temperature doesn't rise after that , Please use... In this position 0 Instead of .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/daily-temperatures
2 Title Example
Example 1:
Input : temperatures = [73,74,75,71,69,72,76,73]
Output : [1,1,4,2,1,1,0,0]
Example 2:
Input : temperatures = [30,40,50,60]
Output : [1,1,1,0]
Example 3:
Input : temperatures = [30,60,90]
Output : [1,1,0]
3 Topic tips
1 <= temperatures.length <= 105
30 <= temperatures[i] <= 100
4 Ideas
You can maintain a monotonic stack that stores subscripts , The temperature in the temperature list corresponding to the subscript from the bottom of the stack to the top of the stack decreases in turn . If a subscript is in the monotone stack , Indicates that the next higher temperature subscript has not been found .
Forward traverse temperature list . For each element in the temperature list temperatures[i] , If the stack is empty , Will directly i Into the stack , If the stack is not empty , Compare stack top elements prevIndex Corresponding temperature temperatures[prevIndex] And current temperature temperatures[i], If termperatures[i] > temperaturesLprevIndex] , Will prevIndex remove , And will prevIndex The corresponding waiting days are assigned i -prevIndex , Repeat the above operation until the stack is empty or the temperature corresponding to the top element of the stack is less than or equal to the current temperature , And then i Into the stack .
Why can I update it when I play the stack ans[prevIndex] Well ? Because in this case , About to enter the stack i Corresponding temperatures[i] It must be temperatures[prevIndex] The first larger element on the right , Imagine if prevIndex and i There are elements larger than it , Suppose the subscript is j, that prevIndex It must be in the subscript j the — Wheel bounced off .
Because the monotonic stack satisfies the corresponding temperature decrease from the bottom of the stack to the top of the stack , So every time an element is put on the stack , Will remove all elements with lower temperatures , And update the waiting days corresponding to the stack element , This ensures that the number of waiting days must be minimal .
Here is a specific example to help readers understand monotone stack . For temperature list [73,74,75,71,69,72,76,73], Monotonic stack stack The initial state of is empty , answer ans The initial state of is [0,0,0,0,0,0,0,0], Follow these steps to update the monotone stack and answer , The elements in the monotone stack are subscripts , The numbers in brackets indicate the temperature corresponding to the subscript in the temperature list .

Complexity analysis
Time complexity :O(n), among n Is the length of the temperature list . Forward traverse temperature list — All over , For each subscript in the temperature list , There is at most one operation of entering and exiting the stack .
Spatial complexity :O(n), among n Is the length of the temperature list . You need to maintain a monotone stack to store the subscript in the temperature list .
5 My answer
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;
}
}
边栏推荐
- [openvx] VX for basic use of objects_ distribution
- pip-script.py‘ is not present Verifying transaction: failed
- 【图像分类】2021-MLP-Mixer NIPS
- 搬家通知!
- 203. Remove linked list elements
- 贪心——55. 跳跃游戏
- 8000字讲透OBSA原理与应用实践
- Responsive high-end website template source code Gallery material resource download platform source code
- TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
- 12月份PMP考试首次采用新考纲,该怎么学?
猜你喜欢

Mouse operation and response

12月份PMP考试首次采用新考纲,该怎么学?

高等数学(第七版)同济大学 习题3-5 个人解答
![[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix](/img/6c/b5fd3350886fd74557439f5361e7f8.png)
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix

8000 word explanation of OBSA principle and application practice

Responsive high-end website template source code Gallery material resource download platform source code

最新版宝塔安装zip扩展,php -m 不显示的处理方法

接口自动化测试,完整入门篇

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!

WordPress简约mkBlog博客主题模板v2.1
随机推荐
12月份PMP考试首次采用新考纲,该怎么学?
【P4】解决本地文件修改与库文件间的冲突问题
deepstream 检测结果截图
WordPress简约mkBlog博客主题模板v2.1
LeetCode 0141. 环形链表 - 三种方法解决
【OPENVX】对象基本使用之vx_matrix
Golang gets the tag of the loop nested structure
贪心——122. 买卖股票的最佳时机 II
【OPENVX】对象基本使用之vx_distribution
什么是Tor?Tor浏览器更新有什么用?
Jumping game II in question 45 of C language power deduction. Ergodic jump
Leetcode58. 最后一个单词的长度
LightPicture – 精致图床系统
Unity simply implements the dialog function
LabVIEW loads and uses custom symbols in tree control projects
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
【OPENVX】对象基本使用之vx_image
Greedy - 53. Maximum subarray sum
Interface automation test, complete introduction
Prefix-Tuning: Optimizing Continuous Prompts for Generation