Java Solution


  • 1
        public int[] nextGreaterElement(int[] findNums, int[] nums) {
    
            Map<Integer, Integer> map = new HashMap<>();
            LinkedList<Integer> stack = new LinkedList<>();
    
            for (int i=0; i<nums.length; ++i) {
                while(!stack.isEmpty() && nums[i] > stack.peek()) {
                    map.put(stack.pop(), nums[i]);
                }
    
                stack.push(nums[i]);
            }
    
            return IntStream.of(findNums)
                        .map(num -> map.getOrDefault(num, -1))
                        .toArray();
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.