当前位置:网站首页>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;
}
}
边栏推荐
- 203. Remove linked list elements
- TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely
- 收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
- 【OPENVX】对象基本使用之vx_image
- Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
- 695. Maximum area of the island
- 贪心——55. 跳跃游戏
- The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
- 单调栈——739. 每日温度
- input 上传文件并回显 FileReader并限制选择文件时的类型
猜你喜欢

Volvo: what on earth does the deep-rooted "sense of security" rely on?

《剑指offer》| 刷题小记

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

How to solve MySQL deep paging problem

ASEMI整流桥GBPC3510在直流有刷电机中的妙用

695. Maximum area of the island

Differences among BRD, MRD and PRD

LabVIEW loads and uses custom symbols in tree control projects

4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits

ES6 从入门到精通 # 09:Symbol 类型
随机推荐
A treasure simulates login and reduces the method of secondary verification
Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
D2dengine edible tutorial (4) -- draw text
贪心——53. 最大子数组和
203. Remove linked list elements
动态规划——416. 分割等和子集
WordPress simple mkblog blog theme template v2.1
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions
C语言:不创建临时变量实现两数交换
WordPress简约mkBlog博客主题模板v2.1
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
LabVIEW loads and uses custom symbols in tree control projects
一篇文章掌握Postgresql中对于日期类数据的计算和处理
动态规划——63. 不同路径 II
【OPENVX】对象基本使用之vx_convolution
[openvx] VX for basic use of objects_ convolution
pip-script. py‘ is not present Verifying transaction: failed
Qt:qmessagebox message box, custom signal and slot
[openvx] VX for basic use of objects_ distribution