- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution85.java
More file actions
Latest commit
executable file
·62 lines (56 loc) · 1.96 KB
/
Copy pathSolution85.java
File metadata and controls
executable file
·62 lines (56 loc) · 1.96 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
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
importjava.util.Arrays;
importjava.util.Stack;
/**
* Created by slade on 2019/7/9.
*/
publicclassSolution85 {
publicintmaximalRectangle(char[][] matrix) {
intmatrixHeight = matrix.length;
if (matrixHeight == 0) {
return0;
}
intmatrixWide = matrix[0].length;
intans = 0;
int[] lastValue = newint[matrixWide];
Arrays.fill(lastValue, 0);
for (inti = 0; i < matrixHeight; i++) {
int[] tmp = newint[matrixWide];
for (intj = 0; j < matrixWide; j++) {
if (matrix[i][j] != '0') {
tmp[j] = lastValue[j] + 1;
lastValue[j] = lastValue[j] + 1;
} else {
tmp[j] = 0;
lastValue[j] = 0;
}
}
ans = Math.max(largestRectangleArea(tmp), ans);
}
returnans;
}
publicintlargestRectangleArea(int[] heights) {
intheightsLen = heights.length;
Stack<Integer> stack = newStack<>();
stack.add(-1);
intarea = 0;
for (inti = 0; i < heightsLen; i++) {
while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]) {
inttmpIdx = stack.pop();
inttmpVal = stack.isEmpty() ? -1 : stack.peek();
area = Math.max(area, heights[tmpIdx] * (i - tmpVal - 1));
}
stack.push(i);
}
while (stack.peek() != -1) {
inttmpIdx = stack.pop();
inttmpVal = stack.isEmpty() ? -1 : stack.peek();
area = Math.max(area, heights[tmpIdx] * (heightsLen - tmpVal - 1));
}
returnarea;
}
publicstaticvoidmain(String[] args) {
Solution85s = newSolution85();
char[][] charArray = {{'0', '0', '1'}, {'0', '0', '1'}, {'1', '1', '1'}, {'1', '1', '1'}, {'1', '0', '1'}};
System.out.println(s.maximalRectangle(charArray));
}
}