knakul853

Untitled

Jul 20th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> nextGreaterElements(vector<int>& nums) {
  4.         int n = nums.size();
  5.        
  6.         vector<int>res(n,-1);
  7.        
  8.         stack<int>st;
  9.         for(int i=0;i<2*nums.size();i++)
  10.         {
  11.             while(!st.empty()&& nums[st.top()] < nums[i%n] )
  12.             {
  13.                 res[st.top()] = nums[i%n];
  14.                 st.pop();
  15.             }
  16.            
  17.             st.push(i%n);
  18.         }
  19.         return res;
  20.     }
  21. };
Add Comment
Please, Sign In to add comment