当前位置:网站首页>LeetCode 496. Next larger element I
LeetCode 496. Next larger element I
2022-07-05 09:26:00 【Sasakihaise_】
【 Monotonic stack 】 Go back and forth , For elements i, Keep popping than i Small elements , If the stack is not empty , Then the top of the stack is the first ratio i Big elements , And then put i Push into the stack .
class Solution {
// Monotone stack finds the first element larger than him on the right
// 1:55 2:14
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Deque<Integer> stack = new LinkedList();
Map<Integer, Integer> map = new HashMap();
for (var i = 0; i < nums2.length; i++) {
map.put(nums2[i], i);
}
int[] arr = new int[nums2.length];
for (var i = nums2.length - 1; i >= 0; i--) {
while (!stack.isEmpty()) {
int top = stack.peek();
if (top > nums2[i]) {
arr[i] = top;
stack.push(nums2[i]);
break;
} else {
stack.poll();
}
}
if (stack.isEmpty()) {
arr[i] = -1;
stack.push(nums2[i]);
}
}
int[] ans = new int[nums1.length];
for (var i = 0; i < nums1.length; i++) {
ans[i] = arr[map.get(nums1[i])];
}
return ans;
}
}
边栏推荐
- Analysis of eventbus source code
- 【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
- Unity SKFramework框架(二十三)、MiniMap 小地图工具
- Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
- 顶会论文看图对比学习(GNN+CL)研究趋势
- OpenGL - Lighting
- 浅谈Label Smoothing技术
- Confusion matrix
- [ManageEngine] how to make good use of the report function of OpManager
- Generate confrontation network
猜你喜欢
Understanding rotation matrix R from the perspective of base transformation
Kotlin introductory notes (II) a brief introduction to kotlin functions
Information and entropy, all you want to know is here
Applet data attribute method
nodejs_ fs. writeFile
Newton iterative method (solving nonlinear equations)
编辑器-vi、vim的使用
nodejs_ 01_ fs. readFile
混淆矩阵(Confusion Matrix)
LeetCode 496. 下一个更大元素 I
随机推荐
STM32 simple multi-level menu (array table lookup method)
OpenGL - Coordinate Systems
Figure neural network + comparative learning, where to go next?
Global configuration tabbar
np. allclose
Understanding rotation matrix R from the perspective of base transformation
A detailed explanation of the general process and the latest research trends of map comparative learning (gnn+cl)
Progressive JPEG pictures and related
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
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
Applet (subcontracting)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
编辑器-vi、vim的使用
Node collaboration and publishing
Kotlin introductory notes (I) kotlin variables and non variables
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
notepad++
2011-11-21 training record personal training (III)
What is a firewall? Explanation of basic knowledge of firewall
Kotlin introductory notes (III) kotlin program logic control (if, when)