当前位置:网站首页>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;
}
}
边栏推荐
- Shutter uses overlay to realize global pop-up
- Greendao reported an error in qigsaw, could not init daoconfig
- .NET服务治理之限流中间件-FireflySoft.RateLimit
- notepad++
- Global configuration tabbar
- 阿里云发送短信验证码
- 一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
- 我的一生.
- [reading notes] Figure comparative learning gnn+cl
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
猜你喜欢
Node collaboration and publishing
3D reconstruction open source code summary [keep updated]
编辑器-vi、vim的使用
Information and entropy, all you want to know is here
OpenGL - Coordinate Systems
Blogger article navigation (classified, real-time update, permanent top)
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
nodejs_ fs. writeFile
OpenGL - Model Loading
Editor use of VI and VIM
随机推荐
Talking about the difference between unittest and pytest
Wechat applet obtains household area information
C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
高性能Spark_transformation性能
Kotlin introductory notes (V) classes and objects, inheritance, constructors
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
STM32简易多级菜单(数组查表法)
生成对抗网络
C # image difference comparison: image subtraction (pointer method, high speed)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
C form click event did not respond
顶会论文看图对比学习(GNN+CL)研究趋势
Rebuild my 3D world [open source] [serialization-1]
信息与熵,你想知道的都在这里了
nodejs_ fs. writeFile
3D reconstruction open source code summary [keep updated]
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Confusion matrix
nodejs_ 01_ fs. readFile
Unity SKFramework框架(二十三)、MiniMap 小地图工具