dynamic programming
1 public class Solution { 2 public int maximalRectangle(char[][] matrix) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(matrix == null||matrix.length == 0) 6 return 0; 7 int result = 0; 8 int[][] hist = new int[matrix.length][matrix[0].length]; 9 10 for(int i = 0; i < matrix.length; i++) 11 for(int j = 0; j < matrix[0].length; j++){ 12 if(matrix[i][j] == '1'){ 13 if(j == 0) 14 hist[i][j] = 1; 15 else 16 hist[i][j] = hist[i][j-1] + 1; 17 } 18 } 19 20 for(int i = 0; i < matrix.length; i++){ 21 for(int j = 0; j < matrix[0].length; j++){ 22 if(hist[i][j] > 0){ 23 int column = hist[i][j]; 24 int row = 1; 25 while((i+row-1) < matrix.length){ 26 if(hist[i+row-1][j] == 0) 27 break; 28 column = Math.min(column, hist[i+row-1][j]); 29 result = Math.max(result, column*row); 30 row++; 31 } 32 } 33 } 34 } 35 return result; 36 } 37 }