faster than 95.11% of c++ solutions

vector nextGreaterElement(vector& nums1, vector& nums2) {
unordered_map<int,int> mpp;
stack s;
vector res;
int n=nums2.size();
s.push(nums2[0]);
for(int i=1;i<n;i++)
{
if(s.empty())
{
s.push(nums2[i]);
continue;
}
while(s.empty()==false && nums2[i]>s.top())
{
mpp[s.top()]=nums2[i];
s.pop();
}

        s.push(nums2[i]);
    }
    while(s.empty()==false)
    {
        mpp[s.top()]=-1;
        s.pop();
    }
    for(int i=0;i<nums1.size();i++)
    {
        res.push_back(mpp[nums1[i]]);
    }
    return res;
}
Comments (0)