package leetcode;

/**
 * https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/
 */
public class Problem1292 {
    public int maxSideLength(int[][] mat, int threshold) {
        int maxRow = mat.length;
        int maxCol = maxRow > 0 ? mat[0].length : 0;
        int[][] rowSums = new int[maxRow][maxCol];
        for (int row = 0; row < maxRow; row++) {
            for (int col = 0; col < maxCol; col++) {
                if (col - 1 < 0) {
                    rowSums[row][col] = mat[row][col];
                } else {
                    rowSums[row][col] = rowSums[row][col - 1] + mat[row][col];
                }
            }
        }
        int[][] colSums = new int[maxRow][maxCol];
        for (int col = 0; col < maxCol; col++) {
            for (int row = 0; row < maxRow; row++) {
                if (row - 1 < 0) {
                    colSums[row][col] = mat[row][col];
                } else {
                    colSums[row][col] += colSums[row - 1][col] + mat[row][col];
                }
            }
        }
        int answer = 0;
        int[][][][] sums = new int[maxRow][maxCol][maxRow][maxCol];
        for (int row = maxRow - 1; row >= 0; row--) {
            for (int col = maxCol - 1; col >= 0; col--) {
                sums[row][col][row][col] = mat[row][col];
                answer = Math.max(answer,
                    Math.max(mat[row][col] <= threshold ? 1 : 0,
                        maxSideLength(mat, threshold, maxRow, maxCol, rowSums, colSums,
                        sums, row - 1, col - 1, row, col)));
            }
        }
        return answer;
    }

    private static int maxSideLength(int[][] mat, int threshold, int maxRow, int maxCol,
                                     int[][] rowSums, int[][] colSums, int[][][][] sums,
                                     int row1, int col1, int row2, int col2) {
        if (row1 < 0 || col1 < 0) {
            return 0;
        }
        int row = rowSums[row1][col2] - (col1 - 1 < 0 ? 0 : rowSums[row1][col1 - 1]);
        int col = colSums[row2][col1] - (row1 - 1 < 0 ? 0 : colSums[row1 - 1][col1]);
        int sum = row + col - mat[row1][col1] +
            (row1 + 1 == maxRow || col1 + 1 == maxCol ? 0 : sums[row1 + 1][col1 + 1][row2][col2]);
        sums[row1][col1][row2][col2] = sum;
        return Math.max(sum <= threshold ? row2 - row1 + 1 : 0,
            maxSideLength(mat, threshold, maxRow, maxCol, rowSums, colSums, sums, row1 - 1, col1 - 1, row2, col2));
    }

    public static void main(String[] args) {
        Problem1292 prob = new Problem1292();
        System.out.println(prob.maxSideLength(new int[][]{
            {1,1,3,2,4,3,2},
            {1,1,3,2,4,3,2},
            {1,1,3,2,4,3,2}
        }, 4)); // 2
        System.out.println(prob.maxSideLength(new int[][]{
            {1,1},
            {1,1},
            {1,1}
        }, 4)); // 2
        System.out.println(prob.maxSideLength(new int[][]{
            {2,2,2,2,2},
            {2,2,2,2,2},
            {2,2,2,2,2},
            {2,2,2,2,2}
        }, 1)); // 0
        System.out.println(prob.maxSideLength(new int[][]{
            {1,1,1,1},
            {1,0,0,0},
            {1,0,0,0},
            {1,0,0,0}
        }, 6)); // 3
        System.out.println(prob.maxSideLength(new int[][]{
            {18,70},
            {61,1},
            {25,85},
            {14,40},
            {11,96},
            {97,96},
            {63,45}
        }, 40184)); // 2
        System.out.println(prob.maxSideLength(new int[][]{
            {18,70},
            {61,1}
        }, 4)); // 1
        System.out.println(prob.maxSideLength(new int[][]{
            {18,70},
            {61,5}
        }, 4)); // 0
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -1163,66 +1163,51 @@
     
-int%5B%5D%5B%5D%5B%5D%5B%5D sums = new int%5BmaxRow%5D%5BmaxCol%5D%5BmaxRow%5D%5BmaxCol%5D
+Map%3CString, Integer%3E sums = new HashMap%3C%3E()
 ;%0A  
@@ -1340,30 +1340,37 @@
 sums
-%5Brow%5D%5Bcol%5D%5Brow%5D%5Bcol%5D =
+.put(key(row, col, row, col),
  mat
@@ -1371,32 +1371,33 @@
 ), mat%5Brow%5D%5Bcol%5D
+)
 ;%0A              
@@ -1688,24 +1688,163 @@
 wer;%0A    %7D%0A%0A
+    private static String key(int row1, int col1, int row2, int col2) %7B%0A        return row1 + %22,%22 + col1 + %22,%22 + row2 + %22,%22 + col2;%0A    %7D%0A%0A
     private 
@@ -1995,19 +1995,28 @@
 ms, 
-int%5B%5D%5B%5D%5B%5D%5B%5D
+Map%3CString, Integer%3E
  sum
@@ -2388,82 +2388,39 @@
 1%5D +
-%0A            (row1 + 1 == maxRow %7C%7C col1 + 1 == maxCol ? 0 : sums%5B
+ sums.getOrDefault(key(
 row1 + 1
 %5D%5Bco
@@ -2419,18 +2419,18 @@
  + 1
-%5D%5B
+, 
 col1 + 1
 %5D%5Bro
@@ -2425,29 +2425,32 @@
 col1 + 1
-%5D%5B
+, 
 row2
-%5D%5B
+, 
 col2
-%5D
+), 0
 );%0A     
@@ -2460,38 +2460,46 @@
 sums
-%5B
+.put(key(
 row1
-%5D%5B
+, 
 col1
-%5D%5B
+, 
 row2
-%5D%5B
+, 
 col2
-%5D =
+),
  sum
+)
 ;%0A  

Update problem 1292