-
Notifications
You must be signed in to change notification settings - Fork 23
/
84.go
70 lines (59 loc) · 1019 Bytes
/
84.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package problem84
/*
下面为了好理解,所以将步骤 拆解 分离
*/
func largestRectangleArea(heights []int) int {
ma := 0
if len(heights) < 0 {
return 0
}
var stack []int
for i := 0; i < len(heights); i++ {
if i == 0 {
stack = append(stack, heights[i])
m := maxArea(stack)
ma = max(m, ma)
continue
}
if heights[i-1] > heights[i] {
m := maxArea(stack)
ma = max(m, ma)
stack = changeStack(stack, heights[i])
}
stack = append(stack, heights[i])
if i+1 == len(heights) {
m := maxArea(stack)
ma = max(m, ma)
continue
}
}
return ma
}
func maxArea(h []int) int {
ma := 0
if len(h) == 1 {
ma = h[0]
}
for i := 0; i < len(h); i++ {
m := h[i] * (len(h) - i)
ma = max(m, ma)
}
return ma
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func changeStack(s []int, insert int) []int {
if len(s) == 0 {
return append(s, insert)
}
for i := (len(s) - 1); i >= 0; i-- {
if s[i] > insert {
s[i] = insert
}
}
return s
}