We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 14aacf5 commit cc34905Copy full SHA for cc34905
solution/0496.Next Greater Element I/Solution.java
@@ -0,0 +1,17 @@
1
+class Solution {
2
+ public int[] nextGreaterElement(int[] nums1, int[] nums2) {
3
+ Deque<Integer> stack = new ArrayDeque<>();
4
+ Map<Integer, Integer> map = new HashMap<>();
5
+ for (int num : nums2) {
6
+ while (!stack.isEmpty() && num > stack.peek()) {
7
+ map.put(stack.pop(), num);
8
+ }
9
+ stack.push(num);
10
11
+ int[] res = new int[nums1.length];
12
+ for (int i = 0; i < nums1.length; ++i) {
13
+ res[i] = map.getOrDefault(nums1[i], -1);
14
15
+ return res;
16
17
+}
0 commit comments