-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path85.MaximalRectangle.kt
More file actions
37 lines (36 loc) · 1.3 KB
/
Copy path85.MaximalRectangle.kt
File metadata and controls
37 lines (36 loc) · 1.3 KB
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
class Solution {
fun maximalRectangle(matrix: Array<CharArray>): Int {
if (matrix.isEmpty() || matrix[0].isEmpty()) {
return 0
}
val heights = IntArray(matrix[0].size)
var maxArea = 0
for (row in matrix) {
for ((index, cell) in row.withIndex()) {
heights[index] = if (cell == '1') heights[index] + 1 else 0
}
maxArea = maxOf(maxArea, maxRectangleInHistogram(heights))
}
return maxArea
}
fun maxRectangleInHistogram(heights: IntArray): Int {
val stack = mutableListOf<Int>()
var index = 0
var maxArea = 0
while (index < heights.size) {
if (stack.isEmpty() || heights[index] >= heights[stack.last()]) {
stack.add(index++)
} else {
val top = stack.removeAt(stack.size - 1)
val width = if (stack.isEmpty()) index else index - stack.last() - 1
maxArea = maxOf(maxArea, heights[top] * width)
}
}
while (stack.isNotEmpty()) {
val top = stack.removeAt(stack.size - 1)
val width = if (stack.isEmpty()) index else index - stack.last() - 1
maxArea = maxOf(maxArea, heights[top] * width)
}
return maxArea
}
}