-
Notifications
You must be signed in to change notification settings - Fork 0
/
739.每日温度.ts
69 lines (68 loc) · 1.42 KB
/
739.每日温度.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode.cn id=739 lang=typescript
*
* [739] 每日温度
*
* https://leetcode.cn/problems/daily-temperatures/description/
*
* algorithms
* Medium (69.05%)
* Likes: 1462
* Dislikes: 0
* Total Accepted: 416.8K
* Total Submissions: 603.8K
* Testcase Example: '[73,74,75,71,69,72,76,73]'
*
* 给定一个整数数组 temperatures ,表示每天的温度,返回一个数组 answer ,其中 answer[i] 是指对于第 i
* 天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用 0 来代替。
*
*
*
* 示例 1:
*
*
* 输入: temperatures = [73,74,75,71,69,72,76,73]
* 输出: [1,1,4,2,1,1,0,0]
*
*
* 示例 2:
*
*
* 输入: temperatures = [30,40,50,60]
* 输出: [1,1,1,0]
*
*
* 示例 3:
*
*
* 输入: temperatures = [30,60,90]
* 输出: [1,1,0]
*
*
*
* 提示:
*
*
* 1 <= temperatures.length <= 10^5
* 30 <= temperatures[i] <= 100
*
*
*/
// @lc code=start
function dailyTemperatures(temperatures: number[]): number[] {
const ans: number[] = [];
const stack: number[] = [];
for (let i = temperatures.length - 1; i >= 0; i--) {
while (stack.length !== 0 &&temperatures[i] >= temperatures[stack[stack.length - 1]]) {
stack.pop();
}
if (stack.length === 0) {
ans[i] = 0;
} else {
ans[i] = stack[stack.length - 1] - i;
}
stack.push(i);
}
return ans;
}
// @lc code=end