当前位置:网站首页>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 .
边栏推荐
- Opencv 10 brightness contrast adjustment
- Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
- Rounding in JS
- Summary of innovative ideas of transformer model in CV
- Welcome to blog navigation
- [data analysis and visualization] key points of data mapping 7- over mapping
- [data analysis and visualization] key points of data drawing 6- too many data groups
- 03 认识第一个view组件
- The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
- [thoughts in the essay] mourn for development technology expert Mao Xingyun
猜你喜欢
Laravel permission export
Useful websites for writing papers and studying at ordinary times
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2
Ijkplayer source code ---packetqueue
[data analysis and visualization] key points of data drawing 6- too many data groups
Graph theory, tree based concept
Mp4 playback
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
Leetcode 926. Flip string to monotonically increasing [prefix and]
For loop instead of while loop - for loop instead of while loop
随机推荐
House raiding
Opencv 10 brightness contrast adjustment
Word splitting problem
Perfect square
[reading papers] comparison of deeplobv1-v3 series, brief review
Summary of innovative ideas of transformer model in CV
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion
mysql索引
Surpass the strongest variant of RESNET! Google proposes a new convolution + attention network: coatnet, with an accuracy of 89.77%!
Ijkplayer source code - rendering
Digital IC Design -- FIFO design
Laravel permission export
05 tabbar navigation bar function
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
vant实现移动端的适配
[reading paper] generate confrontation network Gan
Opencvsharp4 handwriting recognition
Superficial understanding of conditional random fields
Ijkplayer source code - setting option 2
Opencv 9 resize size change rotate rotate blur mean (blur)