C++ Solution - Easy to Understand


  • 0
    int search(vector<int> &nums,int index)
    {
        for(int i=index+1;i<nums.size();i++)
        {
            if(nums[i] > nums[index])
                return nums[i];
        }
        for(int i=0;i<index;i++)
        {
            if(nums[i] > nums[index])
                return nums[i];
        }
        return -1;
    }
    vector<int> nextGreaterElements(vector<int>& nums) {
        
        vector<int> ans(nums.size());
        for(int i=0;i<nums.size();i++)
            ans[i] = search(nums,i);
        return ans;
        
    }

Log in to reply
 

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