当前位置:网站首页>Stack: daily temperature
Stack: daily temperature
2022-06-13 02:45:00 【Zeng Qiang】
subject
https://leetcode-cn.com/problems/iIQa4I/
Their thinking
Save the subscript of daily temperature in the array with a stack , And maintain a monotonically decreasing stack .
step :
- Push : Traverse daily temperature , If the stack is empty , Save the current temperature to the stack .( Save subscript ).
- Out of the stack : Compare the top element with the current temperature , If the stack top element is less than the current temperature , Out of the stack , Complete the statistics of the last heating days . Continue to stack to determine whether there are days less than the current temperature , Finally, subscript the array of current temperature onto the stack .
- state : As long as the current temperature is greater than the elements in the stack , We'll be out of the stack , So we can find that the stack is always decreasing .
Why choose a data structure : Stack .
Because we need to compare the current temperature with the previous temperature , So this is a derivation process from back to front , Consider choosing a stack as a data set .
Code
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Stack<Integer> stack = new Stack<>();
int[] result = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++) {
while(!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
int preIndex = stack.pop();
result[preIndex] = i - preIndex;
}
stack.push(i);
}
return result;
}
}
summary
Subscript the daily temperature in the array with a stack , Maintain a monotonic decreasing stack .
边栏推荐
- Linked list: delete the penultimate node of the linked list
- [data analysis and visualization] key points of data drawing 9- color selection
- Opencvsharp4 handwriting recognition
- Data processing in detailed machine learning (II) -- Feature Normalization
- [reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2
- Using binary heap to implement priority queue
- Prometheus node_ Exporter installs and registers as a service
- House raiding
- A wechat app for shopping
- How to select fund products? What kind of fund is a good fund?
猜你喜欢

Summary of innovative ideas of transformer model in CV

Introduction and download of common data sets for in-depth learning (with network disk link)
![[reading papers] dcgan, the combination of generating countermeasure network and deep convolution](/img/31/8c225627177169f1a3d6c48fd7e97e.jpg)
[reading papers] dcgan, the combination of generating countermeasure network and deep convolution

How did you spend your winter vacation perfectly?

AAR packaging and confusion
![[data analysis and visualization] key points of data drawing 9- color selection](/img/76/0d707b3d2446b5cd8002cbc2834c5c.jpg)
[data analysis and visualization] key points of data drawing 9- color selection
![[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP](/img/4e/a5c6b1a8880209f89d6bf252ff889a.jpg)
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP
![[thoughts in the essay] mourn for development technology expert Mao Xingyun](/img/6b/d1ef40855fc5ba8275dc624ed61dc2.jpg)
[thoughts in the essay] mourn for development technology expert Mao Xingyun

js 解构赋值

数仓笔记|针对客户维度建模需要关注的5个因素
随机推荐
Opencvsharp4 handwriting recognition
Opencv 17 face recognition
Useful websites for writing papers and studying at ordinary times
String: number of substring palindromes
How did you spend your winter vacation perfectly?
[reading papers] comparison of deeplobv1-v3 series, brief review
04路由跳转并携带参数
Leetcode 926. Flip string to monotonically increasing [prefix and]
03 认识第一个view组件
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
Advanced stair climbing
Uni app Foundation
[reading papers] visual convolution zfnet
too old resource version,Code:410
js 解构赋值
Modify the color of El input, textarea and El checkbox when they are disabled
Rough understanding of wechat cloud development
CV 06 demonstrates backgroundworker
小程序 input,textarea组件权重比fixed的z-index都高
wx.createSelectorQuery()在components获取Dom节点的使用