-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGreaterElement1.java
More file actions
29 lines (25 loc) · 937 Bytes
/
NextGreaterElement1.java
File metadata and controls
29 lines (25 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
/*
* https://leetcode.com/problems/next-greater-element-i/
*/
public class NextGreaterElement1 {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> decreasingStack = new Stack<>();
Map<Integer, Integer> nextGreater = new HashMap<>();
for (int num : nums2) {
while (!decreasingStack.isEmpty() && decreasingStack.peek() < num) {
nextGreater.put(decreasingStack.pop(), num);
}
decreasingStack.push(num);
}
return Arrays.stream(nums1)
.map(key -> Optional.ofNullable(nextGreater.get(key)).orElse(-1))
.toArray();
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new NextGreaterElement1().nextGreaterElement(
new int[]{4, 1, 2},
new int[]{1, 3, 4, 2}
))); // [-1, 3, -1]
}
}