-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextGreaterElementI.java
49 lines (44 loc) · 1.46 KB
/
NextGreaterElementI.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package stack;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
//https://leetcode.com/problems/next-greater-element-i/
public class NextGreaterElementI {
public static void main(String[] args) {
}
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> secondArrayMap = new HashMap<>();
Stack<Integer> solStack = new Stack<>();
for (int i = 0; i < nums2.length; i++) {
secondArrayMap.put(i, nums2[i]);
}
int currentHigest = 0;
for (int i = 0; i < nums1.length; i++) {
int n = nums1[i];
if (currentHigest > n) {
currentHigest = n;
}
for (int j = i; j < nums2.length; j++) {
int n2 = secondArrayMap.get(j);
boolean foundGreaterElement = false;
if (n2 > currentHigest) {
solStack.push(n2);
foundGreaterElement = true;
break;
}
if (!foundGreaterElement) {
solStack.push(-1);
}
}
}
int sol[] = new int[nums1.length];
int totalSolutionElement = nums1.length;
while (totalSolutionElement != 0) {
if (!solStack.isEmpty()) {
sol[totalSolutionElement - 1] = solStack.pop();
}
totalSolutionElement--;
}
return sol;
}
}