-
Notifications
You must be signed in to change notification settings - Fork 23
/
85.go
105 lines (90 loc) · 1.47 KB
/
85.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package problem85
/*
此题 为84题的升级版
*/
func maximalRectangle(mat [][]byte) int {
m := len(mat)
if m == 0 {
return 0
}
n := len(mat[0])
if n == 0 {
return 0
}
dp := make([][]int, m)
for i := 0; i < m; i++ {
dp[i] = make([]int, n)
}
for j := 0; j < n; j++ {
dp[0][j] = int(mat[0][j] - '0')
for i := 1; i < m; i++ {
if mat[i][j] == '1' {
dp[i][j] = dp[i-1][j] + 1
}
}
}
max := 0
for i := 0; i < m; i++ {
tmp := largestRectangleArea(dp[i])
if max < tmp {
max = tmp
}
}
return max
}
// 从 84 题复制
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
}