cl1p.net - The internet clipboard
cl1p.net Discord
Login/Sign Up
cl1p.net/code
cl1p.net/code
cl1p.net Discord
Login/Sign Up
This cl1p will be deleted in in 6 hours.
Copy
class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] nge = new int[nums2.length]; Stack<Integer> st = new Stack<>(); for (int i = nums2.length - 1; i >= 0; i--) { while (!st.isEmpty() && st.peek() <= nums2[i]) { st.pop(); } if (st.isEmpty()) { nge[i] = -1; } else { nge[i] = st.peek(); } st.push(nums2[i]); } HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums2.length; i++) { map.put(nums2[i], nge[i]); } int[] ans = new int[nums1.length]; for (int i = 0; i < nums1.length; i++) { ans[i] = map.get(nums1[i]); } return ans; } }