Easy java solution, O(n^2)


  • 0
    I
    public int[] nextGreaterElements(int[] nums) {
            if(nums==null || nums.length==0)
                return new int[0];
            int len = nums.length;
            int[] flatten = new int[len*2-1];
            for(int i=0;i<len;i++){
                flatten[i] = nums[i];
            }
            int t = len;
            for(int i=0;i<len-1;i++)    
                flatten[t++] = nums[i];
            int[] res = new int[len];
            for(int i=0;i<len;i++){
            	int k=i+1;
                for(;k<flatten.length;k++){
                    if(flatten[k]>nums[i]){
                        res[i]= flatten[k];
                    	break;
                    }
                }
                if(k==flatten.length)
                	res[i] = -1;
            }
            return res;
        }
    

Log in to reply
 

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