Sainnhe Park
ac57b13edb
All checks were successful
continuous-integration/drone/push Build is passing
28 lines
666 B
C++
28 lines
666 B
C++
#include "s0739_daily_temperatures.hpp"
|
|
|
|
vector<int> S0739::dailyTemperatures(vector<int>& temperatures) {
|
|
int len = temperatures.size();
|
|
if (len == 0) {
|
|
return vector<int> {};
|
|
} else if (len == 1) {
|
|
return vector<int> {0};
|
|
}
|
|
// 栈中的元素为向量的下标
|
|
// 向量下标对应的元素从栈底到栈顶单调递增
|
|
stack<int> s;
|
|
s.push(0);
|
|
// 初始化 ans 中的所有元素为 0
|
|
vector<int> ans(len, 0);
|
|
for (int i{1}; i < len; ++i) {
|
|
while (temperatures[i] > temperatures[s.top()]) {
|
|
ans[s.top()] = i - s.top();
|
|
s.pop();
|
|
if (s.empty()) {
|
|
break;
|
|
}
|
|
}
|
|
s.push(i);
|
|
}
|
|
return ans;
|
|
}
|