Efficient Java solution


  • 0
    L
      public int[] nextGreaterElement(int[] findNums, int[] nums) {
            HashMap<Integer,Integer> map = new HashMap<>();
    
            for(int i=0;i<nums.length;i++) {
                map.put(nums[i],i);
            }
    
            for(int i=0;i<findNums.length;i++) {
                int max = -1;
                if(map.get(findNums[i])==null) {
                    findNums[i]=max;
                    continue;
                }
                for(int j=map.get(findNums[i])+1;j<nums.length;j++) {
                    if(nums[j]>findNums[i]) {
                        max = nums[j];
                        break;
                    }
                }
                findNums[i]=max;
                
            }
            return findNums;
        }
    

Log in to reply
 

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