stack solution


  • 0
    J
    class Solution {
    public:
        vector<int> nextGreaterElements(vector<int>& nums) {
            vector<int> res(nums.size(),-1);
            stack<int> stk;
            for(int i = 0;i<nums.size()*2;i++)
            {
               int num = nums[i%nums.size()];
               while(!stk.empty() && nums[stk.top()]<num)
               {
                   res[stk.top()]= num;
                   stk.pop();
               }
               if(i<nums.size()) stk.push(i);
            }
            return res;
        }
    };
    

Log in to reply
 

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