Skip to content

Latest commit

 

History

History
29 lines (19 loc) · 593 Bytes

4.md

File metadata and controls

29 lines (19 loc) · 593 Bytes

Method 1 (O(n^2) TLE)

METHOD 2 (using stack)

vector<int> dailyTemperatures(vector<int>& temperatures) {

        int n=temperatures.size();
        vector<int>ans(n,0);

        stack<int>s;

        for(int i=n-1;i>=0;i--)
        {
            while(!s.empty() && temperatures[s.top()]<=temperatures[i])
                s.pop();

            if(!s.empty())
                ans[i]=s.top()-i;

            s.push(i);            

        }
        return ans;
    }