739. Daily Temperatures

PHOTO EMBED

Sun Dec 18 2022 06:20:46 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& t) {
        vector<int> ans;
        stack<int> s;
        int n=t.size();
        for(int i=n-1;i>=0;i--)
        {
            if(s.empty())
            {
                s.push(i);
                ans.push_back(0);
            }
            else 
            {
                while(!s.empty() && t[s.top()]<=t[i])
                {
                    s.pop();
                }
                if(s.empty())
                { 
                    s.push(i);
                    ans.push_back(0);
                }
                else if(!s.empty()&&t[s.top()]>t[i])
                {
                    ans.push_back(s.top()-i);
                    s.push(i);
                }
            }
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};
content_copyCOPY

https://leetcode.com/problems/daily-temperatures/description/