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
// @Title: 每日温度 (Daily Temperatures)
// @Author: 15816537946@163.com
// @Date: 2019-09-16 22:18:56
// @Runtime: 2084 ms
// @Memory: 10.3 MB
/*
 * @lc app=leetcode.cn id=739 lang=golang
 *
 * [739] 每日温度
 *
 * https://leetcode-cn.com/problems/daily-temperatures/description/
 *
 * algorithms
 * Medium (55.63%)
 * Likes:    153
 * Dislikes: 0
 * Total Accepted:    13.5K
 * Total Submissions: 24.4K
 * Testcase Example:  '[73,74,75,71,69,72,76,73]'
 *
 * 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。
 *
 * 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4,
 * 2, 1, 1, 0, 0]。
 *
 * 提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。
 *
 */
func dailyTemperatures(temperatures []int) []int {
	n := len(temperatures)
	res := make([]int, n)

	stack := make([]int, n)

	top := -1
	for i := 0; i < n; i++ {
		for top >= 0 && temperatures[stack[top]] < temperatures[i] {
			res[stack[top]] = i - stack[top]
			top--
		}

		top++
		stack[top] = i
	}

	return res
}