python:
class Solution:
def dailyTemperatures(self, temperatures):
"""
:type temperatures: List[int]
:rtype: List[int]
"""
stack = [[temperatures[0], 0]]
res = [0] * len(temperatures)
for i in range(1, len(temperatures)):
while stack and stack[-1][0] < temperatures[i]:
val, key = stack.pop()
res[key] = i - key
stack.append([temperatures[i], i])
return res
c++:
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<pair<int, int>> stack;
int l = temperatures.size();
vector<int> res(l, 0);
for (int i=l-1; i>=0; i--){
while (!stack.empty() && stack.back().first <= temperatures[i]) stack.pop_back();
res[i] = stack.empty()? 0:stack.back().second - i;
stack.push_back({temperatures[i], i});
}
return res;
}
};