add: 每日温度

This commit is contained in:
2020-06-11 13:32:29 +08:00
parent 4fca08df37
commit ada2de5c1a
3 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,18 @@
/**
* @param {number[]} T
* @return {number[]}
*/
export const dailyTemperatures = function (T) {
const len = T.length
const ans = new Array(len).fill(0)
const stack = []
for (let i = 0; i < len; i++) {
const temperature = T[i]
while (stack.length && temperature > T[stack[stack.length - 1]]) {
const prevIndex = stack.pop()
ans[prevIndex] = i - prevIndex
}
stack.push(i)
}
return ans
}