题目地址:链接
思路: 单调栈(递减)。每日想要知道下一个更高温,可以维护一个单调栈。如果有一个数覆盖当前值,则为下一个更高温日。
function dailyTemperatures(temperatures: number[]): number[] {
let n = temperatures.length;
let stk = [];
let ans = new Array(n).fill(0);
for(let i = 0; i < n; i ++ ) {
let tep = temperatures[i];
if(!stk.length) {
stk.push([i, tep]);
continue;
}
while(stk.length && stk[stk.length - 1][1] < tep) {
let [idx, num] = stk.pop();
ans[idx] = i - idx;
}
stk.push([i, tep]);
}
return ans;
};
8万+

被折叠的 条评论
为什么被折叠?



