当前位置:网站首页>LeetCode 503. Next bigger Element II
LeetCode 503. Next bigger Element II
2022-07-05 09:26:00 【Sasakihaise_】

【 Monotonic stack 】 Put those elements before the last element on the stack first , Form a ring , Then follow the monotonic stack method to traverse :
That is, for each element i, Put all the elements smaller than him out of the stack , At this time, if there is still , Then the top of the stack is the first one bigger than him , If it is empty, it means there is no . Finally, don't forget to put the elements i Push .
class Solution {
// Monotonic stack 3:15 3:24
public int[] nextGreaterElements(int[] nums) {
Deque<Integer> stack = new LinkedList();
int i, j, n = nums.length;
for (i = n - 2; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() <= nums[i]) {
stack.pop();
}
stack.push(nums[i]);
}
int[] ans = new int[n];
for (i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peek() <= nums[i]) {
stack.pop();
}
if (stack.isEmpty()) {
ans[i] = -1;
} else {
ans[i] = stack.peek();
}
stack.push(nums[i]);
}
return ans;
}
}
边栏推荐
- Thermometer based on STM32 single chip microcomputer (with face detection)
- Talking about label smoothing technology
- Information and entropy, all you want to know is here
- It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future
- What is a firewall? Explanation of basic knowledge of firewall
- 一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
- 信息與熵,你想知道的都在這裏了
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- 22-07-04 西安 尚好房-项目经验总结(01)
- Figure neural network + comparative learning, where to go next?
猜你喜欢

C language - input array two-dimensional array a from the keyboard, and put 3 in a × 5. The elements in the third column of the matrix are moved to the left to the 0 column, and the element rows in ea

【阅读笔记】图对比学习 GNN+CL
![[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b](/img/ee/8e07e2dd89bed63ff44400fe1864a9.jpg)
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b

OpenGL - Lighting
![Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]](/img/7d/e7370e757c18b3dbb47e633703c346.jpg)
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!

信息与熵,你想知道的都在这里了

Huber Loss

Kotlin introductory notes (VII) data class and singleton class

Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
随机推荐
Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
什么是防火墙?防火墙基础知识讲解
2011-11-21 training record personal training (III)
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
Kotlin introductory notes (V) classes and objects, inheritance, constructors
Composition of applet code
nodejs_ fs. writeFile
Alibaba's ten-year test brings you into the world of APP testing
编辑器-vi、vim的使用
MYSQL 对字符串类型排序不生效问题
[reading notes] Figure comparative learning gnn+cl
优先级队列(堆)
Kotlin introductory notes (I) kotlin variables and non variables
[ManageEngine] how to make good use of the report function of OpManager
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
2311. 小于等于 K 的最长二进制子序列
Using request headers to develop multi terminal applications
MySQL does not take effect in sorting string types
Kotlin introductory notes (VIII) collection and traversal
SMT32H7系列DMA和DMAMUX的一点理解