当前位置:网站首页>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;
}
}
边栏推荐
- Rebuild my 3D world [open source] [serialization-1]
- Rebuild my 3D world [open source] [serialization-2]
- 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
- OpenGL - Lighting
- [reading notes] Figure comparative learning gnn+cl
- C form click event did not respond
- Creation and reference of applet
- Wxss template syntax
- Return of missing persons
- Blogger article navigation (classified, real-time update, permanent top)
猜你喜欢
![Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]](/img/d8/39020b1ce174299f60b6f278ae0b91.jpg)
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]

Huber Loss

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

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

优先级队列(堆)

Nodemon installation and use

Wxml template syntax

The research trend of map based comparative learning (gnn+cl) in the top paper
![Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]](/img/c4/27ae0d259abc4e61286c1f4d90c06a.png)
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]

C form click event did not respond
随机推荐
Global configuration tabbar
Talking about label smoothing technology
Huber Loss
Progressive JPEG pictures and related
Attention is all you need
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
LeetCode 31. 下一个排列
The research trend of map based comparative learning (gnn+cl) in the top paper
【ManageEngine】如何利用好OpManager的报表功能
C # compare the differences between the two images
信息與熵,你想知道的都在這裏了
我的一生.
Information and entropy, all you want to know is here
一次 Keepalived 高可用的事故,让我重学了一遍它
OpenGL - Model Loading
顶会论文看图对比学习(GNN+CL)研究趋势
Unity SKFramework框架(二十四)、Avatar Controller 第三人称控制
STM32 simple multi-level menu (array table lookup method)
2311. Longest binary subsequence less than or equal to K
Applet (subcontracting)