JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 }

 

posted on 2013-11-18 16:32  JasonChang  阅读(182)  评论(0编辑  收藏  举报