Very concise c++ code using index stack


  • 0
    F
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> highToLowStk;
        int n = temperatures.size();
        vector<int> ans(n, 0);
        for(int i=0; i<n; ++i) {
            while(!highToLowStk.empty() && temperatures[highToLowStk.top()] < temperatures[i]) {
                int idx = highToLowStk.top();
                ans[idx] = i - idx;
                highToLowStk.pop();
            }
            highToLowStk.push(i);
        }
        return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.