<p>Given an array of integers <code>temperatures</code> represents the daily temperatures, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is the number of days you have to wait after the</em> <code>i<sup>th</sup></code> <em>day to get a warmer temperature</em>. If there is no future day for which this is possible, keep <code>answer[i] == 0</code> instead.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> temperatures = [73,74,75,71,69,72,76,73]
<strong>Output:</strong> [1,1,4,2,1,1,0,0]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> temperatures = [30,40,50,60]
<strong>Output:</strong> [1,1,1,0]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> temperatures = [30,60,90]
<strong>Output:</strong> [1,1,0]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;=&nbsp;temperatures.length &lt;= 10<sup>5</sup></code></li>
	<li><code>30 &lt;=&nbsp;temperatures[i] &lt;= 100</code></li>
</ul>

class Solution {
    struct Point {
        int temp;
        int idx;
    };

public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        /* O(nlogn) */
        /*auto ptCmp = [](const Point& p1, const Point& p2) -> bool { return p2.temp < p1.temp; };
        priority_queue<Point, vector<Point>, decltype(ptCmp)> pq;
        vector<int> res(temperatures.size(), 0);
        for(int i = 0; i < res.size(); i++) {
            if(i < res.size() - 1) {
                if(temperatures[i] < temperatures[i+1]) {
                    res[i] = 1;
                } else {
                    pq.push({temperatures[i], i});
                }
            }
            while(!pq.empty() && pq.top().temp < temperatures[i]) {
                res[pq.top().idx] = i - pq.top().idx;
                pq.pop();
            }
        }
        return res;*/
        vector<int> res(temperatures.size(), 0);
        stack<int> stk;

        for(int i = 0; i < res.size(); i++) {
            while(!stk.empty() && temperatures[stk.top()] < temperatures[i]) {
                res[stk.top()] = i - stk.top();
                stk.pop();
            }
            if(i < res.size() - 1) {
                if(temperatures[i] < temperatures[i+1]) {
                    res[i] = 1;
                } else {
                    stk.push(i);
                }
            }
        }

        return res;
    }
};
102760