Daily Temperatures Problem & Solution

See the daily temperatures problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#pragma GCC ivdep

static const int _=[]{ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

class Solution {
public:
  vector<int> dailyTemperatures(vector<int>& temperatures) {
    vector<int> results(temperatures.size());
    stack<pair<int, int>> st;

    for (int i = 0; i < results.size(); ++i) {
      pair<int, int> top;
      while (!st.empty() && (top = st.top()).second < temperatures[i]) {
        results[top.first] = i - top.first;

        st.pop();
      }
      
      st.push({i, temperatures[i]});
    }
    
    return results;
  }
};

Start Here

Many paths, there are. Follow yours, you must.