vector<int> v;
stack<pair<int,int>> s; //first is ngl and second is its index
for(int i=0;i<size;i++)
{
if(s.size()==0)
{
v.push_back(-1);
}
else if(s.size()>0 && s.top().first<=arr[i])
{
v.push_back(s.top().second)
}
else if(s.size()>0 && s.top().first<=arr[i])
{
while(s.size()>0 && s.top().first<=arr[i])
{
s.pop();
}
if(s.size()==0)
v.push_back(-1);
else v.push_back(s.top().second)
}
s.push({arr[i],i});
}
return v; //NGL we got
for(int i=0;i<v.size();i++)
{
v[i]=i-v[i];
}
return v; //final answer
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter