单调栈
遍历温度列表
如果栈s空,则i入栈;
否则,如果当前温度temperatures[i] > temperatures[s.top()
,弹出栈顶,并将对应的等待天数赋为i - t
C++ 代码
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n);
stack<int> s;
for(int i = 0; i < n; ++i) {
while (s.size() && temperatures[i] > temperatures[s.top()])
{
int t = s.top();
res[t] = i - t;
s.pop();
}
s.push(i);
}
return res;
}
};