leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0503.cpp (622B)


      1 class Solution {
      2   public:
      3     vector<int> nextGreaterElements(vector<int> &nums) {
      4         vector<int> res(nums.size(), -1);
      5         stack<int> st;
      6 
      7         for (int i = 0; i < nums.size(); i++) {
      8             while (!st.empty() && nums[i] > nums[st.top()]) {
      9                 res[st.top()] = nums[i];
     10                 st.pop();
     11             }
     12             st.push(i);
     13         }
     14 
     15         for (int i = 0; !st.empty() && i < nums.size(); i++) {
     16             while (!st.empty() && nums[i] > nums[st.top()]) {
     17                 res[st.top()] = nums[i];
     18                 st.pop();
     19             }
     20         }
     21 
     22         return res;
     23     }
     24 };