share my js DP solution


  • 0
    T

    /**

    • @param {character[][]} matrix

    • @return {number}
      */
      var maximalRectangle = function(matrix) {
      var rows = matrix.length;
      if(rows == 0)
      return 0;
      var cols = matrix[0].length;

      var result = 0;
      var dp = [];

      for(var i = 0;i<rows;i++){
      dp.push(Array(cols).fill(0));
      for(var j = 0;j<cols;j++){

           if(matrix[i][j] == '0'){
               dp[i][j] = 0;
           }else{
               dp[i][j] = (j == 0? 1 : dp[i][j-1]+1);
           }
           
           var k = i;
           var w = j+1;
           while(k>=0 && dp[k][j] != 0){
               w = Math.min(w,dp[k][j]);
               
               result = Math.max(result,w*(i-k+1));
               k--;
           }
           
       }
      

      }

      return result;
      };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.