public class Solution {
private void nextGreater(int[] nums, Map<Integer, Integer> map) {
int len = nums.length;
Deque<Integer> stack = new ArrayDeque<>();
stack.push(nums[0]);
for (int i = 1; i < len; i++) {
while (!stack.isEmpty() && nums[i] > stack.peek()) {
map.put(stack.pop(), nums[i]);
}
stack.push(nums[i]);
}
while (!stack.isEmpty()) {
map.put(stack.pop(), -1);
}
}
public int[] nextGreaterElement(int[] findNums, int[] nums) {
if (findNums == null || nums == null || nums.length < 1 || findNums.length < 1) return new int[0];
HashMap<Integer, Integer> map = new HashMap<>();
nextGreater(nums, map);
int len = findNums.length;
int[] res = new int[len];
for (int i = 0; i < len; i++) {
res[i] = map.get(findNums[i]);
}
return res;
}
}
public class Solution {
public int[] nextGreaterElement(int[] findNums, int[] nums) {
if (nums == null || nums.length == 0) {
return new int[]{};
}
int Length = nums.length;
int findLength = findNums.length;
int[] Final = new int[findLength];
int max = nums[0];
for (int i = 1; i < Length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
int[] Indexer = new int[max + 1];
for (int i = 0; i < Length; i++) {
Indexer[nums[i]] = i;
}
boolean Found = false;
int cur, curindex;
for (int i = 0; i < findLength; i++) {
Found = false;
cur = findNums[i];
curindex = Indexer[cur] + 1;
Final[i] = -1;
if (cur != max) {
while (curindex < Length && Found != true) {
if (nums[curindex] > cur) {
Found = true;
Final[i] = nums[curindex];
}
curindex++;
}
}
}
return Final;
}
}