当前位置:网站首页>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;
}
}
边栏推荐
- Kotlin introductory notes (II) a brief introduction to kotlin functions
- Kotlin introductory notes (VIII) collection and traversal
- 3D reconstruction open source code summary [keep updated]
- [reading notes] Figure comparative learning gnn+cl
- 2311. 小于等于 K 的最长二进制子序列
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- 牛顿迭代法(解非线性方程)
- LeetCode 556. 下一个更大元素 III
- Understanding rotation matrix R from the perspective of base transformation
- Wxml template syntax
猜你喜欢
![[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

Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要

Blogger article navigation (classified, real-time update, permanent top)
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

Node collaboration and publishing

Figure neural network + comparative learning, where to go next?

Progressive JPEG pictures and related
![Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]](/img/68/6bfa390b0bedcdbc4afba2f9bd9c0f.jpg)
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]

C form click event did not respond

编辑器-vi、vim的使用
随机推荐
Kotlin introductory notes (VIII) collection and traversal
Uni app implements global variables
Kotlin introductory notes (VI) interface and function visibility modifiers
Transfer learning and domain adaptation
[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
.NET服务治理之限流中间件-FireflySoft.RateLimit
LeetCode 496. 下一个更大元素 I
Rebuild my 3D world [open source] [serialization-1]
Generate confrontation network
Codeworks round 681 (Div. 2) supplement
Composition of applet code
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
Shutter uses overlay to realize global pop-up
MySQL does not take effect in sorting string types
Kotlin introductory notes (IV) circular statements (simple explanation of while, for)
图神经网络+对比学习,下一步去哪?
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
My life
Svgo v3.9.0+