package leetcode;

/**
 * https://leetcode.com/problems/maximal-square/
 */
public class Problem221 {
    public int maximalSquare(char[][] matrix) {
        // TODO:
        return 0;
    }

    public static void main(String[] args) {
        Problem221 prob = new Problem221();
        int result = prob.maximalSquare(new char[][]{
           new char[]{'1', '0', '1', '0', '0'},
           new char[]{'1', '0', '1', '1', '1'},
           new char[]{'1', '1', '1', '1', '1'},
           new char[]{'1', '0', '0', '1', '0'},
        });
        System.out.println(result); // 4
    }
}

@@ -155,232 +155,945 @@
     
-// TODO:%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem221 prob = new Problem221();%0A        int result = prob.maximalSquare(new char%5B%5D%5B%5D%7B%0A           new char%5B%5D%7B'1', '0', '1', '0', '0'%7D,
+int max = Integer.MIN_VALUE;%0A        for (int row = 0; row %3C matrix.length; row++) %7B%0A            for (int col = 0; col %3C matrix%5Brow%5D.length; col++) %7B%0A                int x1 = col;%0A                int y1 = row;%0A                int x2 = x1;%0A                int y2 = y1;%0A                while (y2 %3C matrix.length && x2 %3C matrix%5Brow%5D.length) %7B%0A                    Square square = isSquare(matrix, x1, y1, x2, y2);%0A                    if (!square.square) %7B%0A                        break;%0A                    %7D%0A                    max = Math.max(max, square.val);%0A                    x2++;%0A                    y2++;%0A                %7D%0A            %7D%0A        %7D%0A        if (max == Integer.MIN_VALUE) %7B%0A            return 0;%0A        %7D%0A        return max;%0A    %7D%0A%0A    private static class Square %7B%0A        boolean square;%0A        int val;%0A%0A        public Square(boolean square, int val) %7B%0A            this.square = square;%0A            this.val = val;
 %0A   
@@ -1101,143 +1101,396 @@
     
+%7D%0A
    
-new char%5B%5D%7B'1', '0', '1', '1', '1'%7D,%0A           new char%5B%5D%7B'1', '1', '1', '1', '1'%7D,%0A           new char%5B%5D%7B'1', '0', '0', '1', '0'%7D,
+ %7D%0A%0A    private Square isSquare(char%5B%5D%5B%5D matrix, int x1, int y1, int x2, int y2) %7B%0A        int sum = 0;%0A        for (int row = y1; row %3C= y2; row++) %7B%0A            for (int col = x1; col %3C= x2; col++) %7B%0A                if (matrix%5Brow%5D%5Bcol%5D != '1') %7B%0A                    return new Square(false, sum);%0A                %7D else %7B%0A                    sum++;%0A                %7D%0A            %7D
 %0A   
@@ -1495,18 +1495,16 @@
        %7D
-);
 %0A       
@@ -1508,40 +1508,37 @@
     
-System.out.println(result); // 4
+return new Square(true, sum);
 %0A   

Solve problem 221