Skip to main content

739. Daily Temperatures

分析


题解

function dailyTemperatures(temperatures: number[]): number[] {
const ans: number[] = []

const monoStack: number[] = []

for (let i = temperatures.length - 1; i >= 0; i--) {
while (monoStack.length && temperatures[monoStack[monoStack.length - 1]] <= temperatures[i]) {
monoStack.pop()
}

ans[i] = monoStack.length ? monoStack[monoStack.length - 1] - i : 0

monoStack.push(i)
}

return ans
};