class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { if (nums1 == null || nums1.length == 0) return new int[0]; Deque<Integer> stack = new LinkedList<>(); Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums2.length; i++) { while (!stack.isEmpty() && nums2[i] > stack.peek()) { map.put(stack.pop(), nums2[i]); } stack.push(nums2[i]); } while (!stack.isEmpty()) { map.put(stack.pop(), -1); } int[] ans = new int[nums1.length]; for (int i = 0; i < nums1.length; i++) { ans[i] = map.get(nums1[i]); } return ans; }}