Advertisement
Guest User

Untitled

a guest
Nov 30th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1.  
  2. public class Solution {
  3.     public int maximalRectangle(char[][] matrix) {
  4.  
  5.         int max = 0;
  6.         for (int i = 0; i < matrix.length; i++) {
  7.             for (int j = 0; j < matrix[0].length; j++) {
  8.                 int width = 0;
  9.                 int height = 0;
  10.  
  11.                 for (int k = i; k < matrix.length && matrix[k][j] == '1'; k++) {
  12.                     width++;
  13.                 }
  14.  
  15.                 for (int k = i; k < i + width; k++) {
  16.                     for (int l = j; l < matrix[0].length; l++) {
  17.                         if (matrix[k][l] == '0') {
  18.                             width = k - i;
  19.                             if (width * height > max) {
  20.                                 max = width * height;
  21.                             }
  22.                         }
  23.                     }
  24.                     height++;
  25.                 }
  26.  
  27.                 if (width * height > max) {
  28.                     max = width * height;
  29.                 }
  30.  
  31.             }
  32.         }
  33.         return max;
  34.     }
  35.  
  36.     public static void main(String[] args) {
  37.  
  38.         Solution solution = new Solution();
  39.         System.out.println(solution.maximalRectangle(new char[][]{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}));
  40.         System.out.println(solution.maximalRectangle(new char[][]{{'1'}, {'0'}}));
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement