zoukankan      html  css  js  c++  java
  • Maximal Rectangle

    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 }
  • 相关阅读:
    关于sifari兼容性的一个问题
    HTML标签的应用(新手)
    HTML标签的应用(新手)
    未完成的开锁动画演示
    HTML标签的应用(新手)
    HTML新手向
    C++
    STL之set
    C++输入输出
    提升一下逼格
  • 原文地址:https://www.cnblogs.com/jasonC/p/3429727.html
Copyright © 2011-2022 走看看