当前位置:网站首页>Leetcode739 daily temperature
Leetcode739 daily temperature
2022-07-02 12:05:00 【Monsters 114】
Given an array of integers temperatures , Indicates the daily temperature , Returns an array answer , among answer[i] It means in the i After heaven , Will have a higher temperature . If the temperature doesn't rise after that , Please use... In this position 0 Instead of .
eg:
Input : temperatures = [73,74,75,71,69,72,76,73] Output : [1,1,4,2,1,1,0,0]
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 temperatures[i] > temperatures[prevIndex], 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 round of was 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 .
When i=0 when , Monotone stack is empty , So it will 0 Into the stack .
stack=[0(73)]
ans=[0,0,0,0,0,0,0,0]
When i=1 when , because 74 Greater than 73, So remove the top element 0, assignment ans[0]=1−0, take 1 Into the stack .
stack=[1(74)]
ans=[1,0,0,0,0,0,0,0]
When i=2 when , because 75 Greater than 74, So remove the top element 11, assignment ans[1]=2−1, take 2 Into the stack .
stack=[2(75)]
ans=[1,1,0,0,0,0,0,0]
When i=3 when , because 71 Less than 75, So it will 3 Into the stack .
stack=[2(75),3(71)]
ans=[1,1,0,0,0,0,0,0]
When i=4 when , because 69 Less than 71, So it will 4 Into the stack .
stack=[2(75),3(71),4(69)]
ans=[1,1,0,0,0,0,0,0]
When i=5 when , because 72 Greater than 69 and 71, So remove the top elements in turn 4 and 3, assignment ans[4]=5−4 and ans[3]=5−3, take 5 Into the stack .
stack=[2(75),5(72)]
ans=[1,1,0,2,1,0,0,0]
When i=6 when , because 76 Greater than 72 and 75, So remove the top elements in turn 5 and 2, assignment ans[5]=6−5 and ans[2]=6-2, take 6 Into the stack .
stack=[6(76)]
ans=[1,1,4,2,1,1,0,0]
When i=7 when , because 73 Less than 76, So it will 7 Into the stack .
stack=[6(76),7(73)]
ans=[1,1,4,2,1,1,0,0]
Code implementation
public int[] dailyTemperatures(int[] t) {
int[] res = new int[t.length];
Stack<Integer> s = new Stack<>();
for(int i = 0 ; i < t.length;i++){
while(!s.isEmpty() && t[i] > t[s.peek()]){
int index = s.pop();
res[index] = i - index;
}
s.push(i);
}
return res;
}边栏推荐
- FLESH-DECT(MedIA 2021)——一个material decomposition的观点
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- 进入前六!博云在中国云管理软件市场销量排行持续上升
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- 小程序链接生成
- Le tutoriel F - String le plus facile à comprendre de l'histoire.
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 基于Arduino和ESP8266的连接手机热点实验(成功)
猜你喜欢

Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

Dynamic memory (advanced 4)

【2022 ACTF-wp】

PyTorch搭建LSTM实现服装分类(FashionMNIST)

GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

xss-labs-master靶场环境搭建与1-6关解题思路

YYGH-BUG-04

测试左移和右移
随机推荐
Data analysis - Matplotlib sample code
Easyexcel and Lombok annotations and commonly used swagger annotations
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
Log4j2
PyTorch搭建LSTM实现服装分类(FashionMNIST)
浅谈sklearn中的数据预处理
Seriation in R: How to Optimally Order Objects in a Data Matrice
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
Dynamic memory (advanced 4)
Fabric.js 3个api设置画布宽高
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Leetcode122 买卖股票的最佳时机 II
easyExcel和lombok注解以及swagger常用注解
字符串回文hash 模板题 O(1)判字符串是否回文
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
YYGH-BUG-05
Leetcode209 长度最小的子数组
HR wonderful dividing line
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic