LeetCode/solutions/739. Daily Temperatures.md
2020-05-07 23:14:33 +08:00

28 lines
864 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/)
# 思路
数组里每个元素的右边第一个比它大的元素若找不到用0填充。典型的单调栈关于单调栈我已经总结过了[戳我](../algorithm/array/monotonic_stack_queue.md)),这里不再赘述,直接给出代码。
时空复杂度均为O(N)
# C++
``` C++
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
int n = T.size();
vector<int>res(T.size(), 0);
stack<int>mono_stk;
for(int i = n - 1; i >= 0; i--){ // 反向遍历
while(!mono_stk.empty() && T[i] >= T[mono_stk.top()])
mono_stk.pop();
if(!mono_stk.empty())
res[i] = mono_stk.top() - i;
mono_stk.push(i);
}
return res;
}
};
```