Easy java O(n^2)


  • 0
    public class Solution {
        public int[] nextGreaterElements(int[] nums) {
            int[] arr = new int[nums.length];
            for (int j = 0; j < nums.length; j++) {
                arr[j] = findNextMax(j, nums);
            }
            return arr;
        }
        private int findNextMax(int index, int[] nums) {
            for (int i = index; i < nums.length; i++) {
                if (nums[i] > nums[index]) return nums[i];
            }
            if (index != 0) {
                for (int i = 0; i < index; i++) {
                    if (nums[i] > nums[index]) return nums[i];
                }
            }
            return -1;
        }
    }
    

Log in to reply
 

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