https://www.youtube.com/watch?v=KZhjUwuMC0Y You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. Output: [-1,3,-1] Explanation: For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. For number 1 in the first array, the next greater number for it in the second array is 3. For number 2 in the first array, there is no next greater number for it in the second array, so output -1. Example 2: Input: nums1 = [2,4], nums2 = [1,2,3,4]. Output: [3,-1] Explanation: For number 2 in the first array, the next greater number for it in the second array is 3. For number 4 in the first array, there is no next greater number for it in the second array, so output -1. // solution 1 // time : n * m // space : n class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; for(int i = 0; i < nums1.length; i++){ res[i] = helper(nums1[i], nums2); } return res; } private int helper(int num, int[] nums2){ int index = 0; // need to have a value for index here (bug 1 ) for(int i = 0; i < nums2.length; i++){ if(nums2[i] == num){ index = i; } } for(int i = index; i < nums2.length; i++){ if(nums2[i] > num){ return nums2[i]; } } return -1; } } // solution 2 , use stack and map, // time : m + n // space : m + n class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { // use a stack to store the possible greater elements from the right // use a map to record the pair, key is a number, value is the number grester than this number from the right // how to use this stack ? // 1 0 3 4 2 // traverse the array from right to left // stack : 2 4 3 // map : key value : 2: -1 , // if the stack is empty, the value should be -1 // stack is not empty, stack.peek() = 2 < nums[i] = 4, pop 2, stack is empty, return -1 for 4 // and then nums [i] = 3, 3 < stack.peek() = 4, so 3 : 4 , push 3 to stack // nums[i] = 0 < stack.peek() = 3 , so 0 : 3 , push 0 to stack , // nums[i] = 1 > stack.peek() = 0, pop 0, nums[i] = 1 < stack.peek() = 3, so 1 : 3 , push 1 to stack // so there are three cases: if the stack is empty, return -1, push the current element into the stack // if the stack.peek() > nums[i], return stack.peek(), push the nums[i] to stack, update map // if the stack.peek() < nums[i], pop stack until reach the above two conditions int[] res = new int[nums1.length]; Stack<Integer> stack = new Stack<>(); HashMap<Integer, Integer> map = new HashMap<>(); for(int i = nums2.length - 1; i >= 0; i--){ if(stack.isEmpty()){ map.put(nums2[i], -1); // stack.push(nums[i]); }else if(stack.peek() > nums2[i]){ map.put(nums2[i], stack.peek()); //stack.push(nums[i]); }else{ // stack.peek() < nums[i] while(!stack.isEmpty() && stack.peek() < nums2[i]){ stack.pop(); } if(stack.isEmpty()){ map.put(nums2[i], -1); }else{ map.put(nums2[i], stack.peek()); } // stack.push(nums[i]); } stack.push(nums2[i]); } for(int i = 0; i < nums1.length; i++){ res[i] = map.get(nums1[i]); } return res; } } // from right to left, so only need to traverse the array 2 once, use a stack to keep the data traversed // better looking code class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; Stack<Integer> stack = new Stack<>(); HashMap<Integer, Integer> map = new HashMap<>(); for(int i = nums2.length - 1; i >= 0; i--){ while(!stack.isEmpty() && stack.peek() < nums2[i]){ stack.pop(); } if(stack.isEmpty()){ map.put(nums2[i], -1); }else{ map.put(nums2[i], stack.peek()); } stack.push(nums2[i]); } for(int i = 0; i < nums1.length; i++){ res[i] = map.get(nums1[i]); } return res; } }