|
| 1 | +## 85. Maximal Rectangle |
| 2 | + |
| 3 | +### Question |
| 4 | +Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. |
| 5 | + |
| 6 | +``` |
| 7 | +Example: |
| 8 | +
|
| 9 | +Input: |
| 10 | +[ |
| 11 | + ["1","0","1","0","0"], |
| 12 | + ["1","0","1","1","1"], |
| 13 | + ["1","1","1","1","1"], |
| 14 | + ["1","0","0","1","0"] |
| 15 | +] |
| 16 | +Output: 6 |
| 17 | +``` |
| 18 | + |
| 19 | +### Thinking: |
| 20 | +* Method 1: Use the method of Monostack as question 84: [84. Largest Rectangle in Histogram](https://seanforfun.github.io/leetcode/2018/11/07/84.LargestRectangleinHistogram.html) |
| 21 | + * we use a height array to save the historgram. |
| 22 | + * if current value is 1, we increase the value saved in that index. |
| 23 | + * if current value is 0, we set the value to 0. |
| 24 | +```Java |
| 25 | +class Solution { |
| 26 | + public int maximalRectangle(char[][] matrix) { |
| 27 | + if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; |
| 28 | + int res = 0; |
| 29 | + int[] height = new int[matrix[0].length + 1]; |
| 30 | + for(int row = 0; row < matrix.length; row++){ |
| 31 | + for(int i = 0; i < matrix[0].length; i++){ |
| 32 | + if(matrix[row][i] == '1') height[i]++; |
| 33 | + else height[i] = 0; |
| 34 | + } |
| 35 | + Stack<Integer> stack = new Stack<>(); |
| 36 | + for(int j = 0; j <= matrix[0].length; j++){ |
| 37 | + while(!stack.isEmpty() && height[j] < height[stack.peek()]){ |
| 38 | + int h = height[stack.pop()]; |
| 39 | + int index = stack.isEmpty() ? -1: stack.peek(); |
| 40 | + res = Math.max(res, h * (j - index - 1)); |
| 41 | + } |
| 42 | + stack.push(j); |
| 43 | + } |
| 44 | + } |
| 45 | + return res; |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +* Method 2: dp |
| 51 | + * we define 3 arrays for each row: |
| 52 | + * left(save the first index of 1 in current row, still need to compare with previous value), initial value is 0. |
| 53 | + * right(save the last index of 1 in current row, need to compare with previous value), initialized with len. |
| 54 | + * height(record the height as historgram) |
| 55 | + * area = height * (right - left) |
| 56 | +```Java |
| 57 | +/** |
| 58 | +Example: |
| 59 | + 0 1 2 3 4 |
| 60 | +row0 ["1","0","1","0","0"], |
| 61 | +row1 ["1","0","1","1","1"], |
| 62 | +row2 ["1","1","1","1","1"], |
| 63 | +row3 ["1","0","0","1","0"] |
| 64 | +
|
| 65 | +height[]: height array saves the historgram for current row. |
| 66 | +row0 [1,0,1,0,0], |
| 67 | +row1 [2,0,2,1,1], |
| 68 | +row2 [3,1,3,2,2], |
| 69 | +row3 [4,0,0,3,0] |
| 70 | +
|
| 71 | +left[]: left array saves the first index in current row and we need to compare it with the previous row left array. |
| 72 | +row0 [0,0,2,0,0], |
| 73 | +row1 [0,0,2,2,2], |
| 74 | +row2 [0,0,2,2,2], |
| 75 | +row3 [0,0,0,3,0] |
| 76 | +
|
| 77 | +right[]: right array saves the last index in current row(need to compare it with the previous row right array.) |
| 78 | +row0 [1,5,3,5,5], |
| 79 | +row1 [1,5,3,5,5], |
| 80 | +row2 [1,5,3,5,5], |
| 81 | +row3 [1,5,5,4,5] |
| 82 | +*/ |
| 83 | +class Solution { |
| 84 | + public int maximalRectangle(char[][] matrix) { |
| 85 | + if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; |
| 86 | + int res = 0, len = matrix[0].length; |
| 87 | + int[] left = new int[len]; |
| 88 | + int[] right = new int[len]; |
| 89 | + int[] height = new int[len]; |
| 90 | + for(int i = 0; i < len; i++) right[i] = len; |
| 91 | + for(int row = 0; row < matrix.length; row++){ |
| 92 | + int cur_left = 0, cur_right = len; |
| 93 | + for(int i = 0; i < len; i++){ |
| 94 | + if(matrix[row][i] == '0') height[i] = 0; |
| 95 | + else height[i]++; |
| 96 | + } |
| 97 | + for(int i = 0; i < len; i++){ |
| 98 | + if(matrix[row][i] == '1'){ |
| 99 | + left[i] = Math.max(left[i], cur_left); |
| 100 | + }else{ |
| 101 | + cur_left = i + 1; |
| 102 | + left[i] = 0; |
| 103 | + } |
| 104 | + } |
| 105 | + for(int i = len - 1; i >= 0; i--){ |
| 106 | + if(matrix[row][i] == '1'){ |
| 107 | + right[i] = Math.min(right[i], cur_right); |
| 108 | + }else{ |
| 109 | + cur_right = i; |
| 110 | + right[i] = len; |
| 111 | + } |
| 112 | + } |
| 113 | + for(int i = 0; i < len; i++){ |
| 114 | + res = Math.max(res, height[i] * (right[i] - left[i])); |
| 115 | + } |
| 116 | + } |
| 117 | + return res; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +### Reference |
| 124 | +1. [Leetcode : 85. Maximal Rectangle 讲解](https://www.youtube.com/watch?v=5CEBM_174e0) |
0 commit comments