|
3 | 3 | import java.util.Arrays;
|
4 | 4 |
|
5 | 5 | /**
|
| 6 | + * 85. Maximal Rectangle |
| 7 | + * |
6 | 8 | * Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
|
7 | 9 |
|
8 | 10 | For example, given the following matrix:
|
|
12 | 14 | 1 1 1 1 1
|
13 | 15 | 1 0 0 1 0
|
14 | 16 | Return 6.
|
| 17 | +
|
15 | 18 | */
|
16 | 19 | public class _85 {
|
| 20 | + public static class Solution1 { |
17 | 21 | public int maximalRectangle(char[][] matrix) {
|
18 |
| - if (matrix.length == 0) { |
19 |
| - return 0; |
20 |
| - } |
21 |
| - int m = matrix.length; |
22 |
| - int n = matrix[0].length; |
23 |
| - int[] left = new int[n]; |
24 |
| - int[] right = new int[n]; |
25 |
| - int[] height = new int[n]; |
26 |
| - Arrays.fill(left, 0); |
27 |
| - Arrays.fill(right, n); |
28 |
| - Arrays.fill(height, 0); |
29 |
| - int maxA = 0; |
30 |
| - for (int i = 0; i < m; i++) { |
31 |
| - int currLeft = 0; |
32 |
| - int currRight = n; |
| 22 | + if (matrix.length == 0) { |
| 23 | + return 0; |
| 24 | + } |
| 25 | + int m = matrix.length; |
| 26 | + int n = matrix[0].length; |
| 27 | + int[] left = new int[n]; |
| 28 | + int[] right = new int[n]; |
| 29 | + int[] height = new int[n]; |
| 30 | + Arrays.fill(left, 0); |
| 31 | + Arrays.fill(right, n); |
| 32 | + Arrays.fill(height, 0); |
| 33 | + int maxA = 0; |
| 34 | + for (int i = 0; i < m; i++) { |
| 35 | + int currLeft = 0; |
| 36 | + int currRight = n; |
33 | 37 |
|
34 |
| - //compute height, this can be achieved from either side |
35 |
| - for (int j = 0; j < n; j++) { |
36 |
| - if (matrix[i][j] == '1') { |
37 |
| - height[j]++; |
38 |
| - } else { |
39 |
| - height[j] = 0; |
40 |
| - } |
41 |
| - } |
| 38 | + //compute height, this can be achieved from either side |
| 39 | + for (int j = 0; j < n; j++) { |
| 40 | + if (matrix[i][j] == '1') { |
| 41 | + height[j]++; |
| 42 | + } else { |
| 43 | + height[j] = 0; |
| 44 | + } |
| 45 | + } |
42 | 46 |
|
43 |
| - //compute left, from left to right |
44 |
| - for (int j = 0; j < n; j++) { |
45 |
| - if (matrix[i][j] == '1') { |
46 |
| - left[j] = Math.max(left[j], currLeft); |
47 |
| - } else { |
48 |
| - left[j] = 0; |
49 |
| - currLeft = j + 1; |
50 |
| - } |
51 |
| - } |
| 47 | + //compute left, from left to right |
| 48 | + for (int j = 0; j < n; j++) { |
| 49 | + if (matrix[i][j] == '1') { |
| 50 | + left[j] = Math.max(left[j], currLeft); |
| 51 | + } else { |
| 52 | + left[j] = 0; |
| 53 | + currLeft = j + 1; |
| 54 | + } |
| 55 | + } |
52 | 56 |
|
53 |
| - //compute right, from right to left |
54 |
| - for (int j = n - 1; j >= 0; j--) { |
55 |
| - if (matrix[i][j] == '1') { |
56 |
| - right[j] = Math.min(right[j], currRight); |
57 |
| - } else { |
58 |
| - right[j] = n; |
59 |
| - currRight = j; |
60 |
| - } |
61 |
| - } |
| 57 | + //compute right, from right to left |
| 58 | + for (int j = n - 1; j >= 0; j--) { |
| 59 | + if (matrix[i][j] == '1') { |
| 60 | + right[j] = Math.min(right[j], currRight); |
| 61 | + } else { |
| 62 | + right[j] = n; |
| 63 | + currRight = j; |
| 64 | + } |
| 65 | + } |
62 | 66 |
|
63 |
| - //compute rectangle area, this can be achieved from either side |
64 |
| - for (int j = 0; j < n; j++) { |
65 |
| - maxA = Math.max(maxA, (right[j] - left[j]) * height[j]); |
66 |
| - } |
| 67 | + //compute rectangle area, this can be achieved from either side |
| 68 | + for (int j = 0; j < n; j++) { |
| 69 | + maxA = Math.max(maxA, (right[j] - left[j]) * height[j]); |
67 | 70 | }
|
68 |
| - return maxA; |
| 71 | + } |
| 72 | + return maxA; |
69 | 73 | }
|
| 74 | + } |
70 | 75 | }
|
0 commit comments