Skip to content

Commit cc34905

Browse files
authored
Create Solution.java
1 parent 14aacf5 commit cc34905

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)