-
Notifications
You must be signed in to change notification settings - Fork 0
/
004.单调栈.go
55 lines (47 loc) · 906 Bytes
/
004.单调栈.go
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
//
// Copyright (C) 2022 lazywhite <[email protected]>
//
// Distributed under terms of the MIT license.
//
package main
import (
"fmt"
)
func main() {
nums := []int{10, 3, 7, 4, 12}
ans := monoStack(nums)
fmt.Println(nums)
fmt.Println(ans)
}
/*
1. 下一个, 从前向后遍历
上一个, 从后向前遍历
2. smarller 弹出比v大的
larger 弹出比v小的
*/
func monoStack(nums []int) []int {
// stack存储num的index
stack := make([]int, 0)
ans := make([]int, len(nums))
for i, v := range nums {
if len(stack) == 0 {
stack = append(stack, i)
} else {
for len(stack) > 0 && nums[stack[len(stack)-1]] > v {
// 更新结果
peek := stack[len(stack)-1]
ans[peek] = v
stack = stack[:len(stack)-1]
}
// 入栈
stack = append(stack, i)
}
}
// stack不为空
if len(stack) > 0 {
for _, idx := range stack {
ans[idx] = -1
}
}
return ans
}