当前位置:网站首页>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;
}边栏推荐
- YYGH-9-预约下单
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Uniapp uni list item @click, uniapp uni list item jump with parameters
- Log4j2
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- Cmake cross compilation
- SVO2系列之深度滤波DepthFilter
- Le tutoriel F - String le plus facile à comprendre de l'histoire.
- Power Spectral Density Estimates Using FFT---MATLAB
- 自然语言处理系列(一)——RNN基础
猜你喜欢

二分刷题记录(洛谷题单)区间的甄别

pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示

How does Premiere (PR) import the preset mogrt template?

K-Means Clustering Visualization in R: Step By Step Guide

From scratch, develop a web office suite (3): mouse events

ESP32 Arduino 引入LVGL 碰到的一些问题

File operation (detailed!)

conda常用命令汇总

Mish-撼动深度学习ReLU激活函数的新继任者

The selected cells in Excel form have the selection effect of cross shading
随机推荐
进入前六!博云在中国云管理软件市场销量排行持续上升
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
XSS labs master shooting range environment construction and 1-6 problem solving ideas
HOW TO ADD P-VALUES TO GGPLOT FACETS
Industry analysis
ORB-SLAM2不同线程间的数据共享与传递
The computer screen is black for no reason, and the brightness cannot be adjusted.
PyTorch nn. Full analysis of RNN parameters
Leetcode122 买卖股票的最佳时机 II
SSH automatically disconnects (pretends to be dead) after a period of no operation
xss-labs-master靶场环境搭建与1-6关解题思路
On data preprocessing in sklearn
[visual studio 2019] create and import cmake project
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
还不会安装WSL 2?看这一篇文章就够了
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
动态内存(进阶四)
Research on and off the Oracle chain
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R