package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/matrix-block-sum/
 */
public class Problem1314 {
    public int[][] matrixBlockSum(int[][] mat, int K) {
        int maxRow = mat.length;
        int maxCol = maxRow > 0 ? mat[0].length : 0;
        int[][] rowSums = new int[maxRow][maxCol];
        int[][] colSums = new int[maxRow][maxCol];
        for (int row = 0; row < maxRow; row++) {
            for (int col = 0; col < maxCol; col++) {
                if (col == 0) {
                    colSums[row][col] = mat[row][col];
                } else {
                    colSums[row][col] = colSums[row][col - 1] + mat[row][col];
                }
                if (row == 0) {
                    rowSums[row][col] = mat[row][col];
                } else {
                    rowSums[row][col] = rowSums[row - 1][col] + mat[row][col];
                }
            }
        }
        int[][] answer = new int[maxRow][maxCol];
        // TODO
        return answer;
    }

    private static void print(int[][] mat) {
        for (int[] m : mat) {
            System.out.println(Arrays.toString(m));
        }
    }

    public static void main(String[] args) {
        Problem1314 prob = new Problem1314();
        print(prob.matrixBlockSum(new int[][]{
            {1,2,3},
            {4,5,6},
            {7,8,9}
        }, 1)); // [[12,21,16],[27,45,33],[24,39,28]]
//        print(prob.matrixBlockSum(new int[][]{
//            {1,2,3},
//            {4,5,6},
//            {7,8,9}
//        }, 2)); // [[45,45,45],[45,45,45],[45,45,45]]
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -260,63 +260,9 @@
 %5D%5B%5D 
-rowSums = new int%5BmaxRow%5D%5BmaxCol%5D;%0A        int%5B%5D%5B%5D colS
+s
 ums 
@@ -434,36 +434,33 @@
                 
-colS
+s
 ums%5Brow%5D%5Bcol%5D = 
@@ -519,20 +519,17 @@
         
-colS
+s
 ums%5Brow%5D
@@ -540,12 +540,9 @@
 %5D = 
-colS
+s
 ums%5B
@@ -606,186 +606,171 @@
     
+%7D%0A
     
-if (row == 0) %7B%0A                    rowSums%5Brow%5D%5Bcol%5D = mat%5Brow%5D%5Bcol%5D;%0A                %7D else %7B%0A                    rowSums%5Brow%5D%5B
+    %7D%0A        int%5B%5D%5B%5D answer = new int%5BmaxRow%5D%5BmaxCol%5D;%0A        for (int row = 0; row %3C maxRow; row++) %7B%0A            for (int 
 col
-%5D
  = 
-rowSums%5Brow - 1%5D%5Bcol%5D + mat%5Brow%5D%5Bcol%5D;
+0; col %3C maxCol; col++) %7B
 %0A   
@@ -786,32 +786,36 @@
     
-%7D%0A     
+int sum = 0;%0A
        
-%7D%0A
         
 %7D%0A  
@@ -814,676 +814,351 @@
     
-%7D%0A        int%5B%5D%5B%5D answe
+ for (int r = Math.max(0, row - K); 
 r 
+%3C
 = 
-new int%5BmaxRow%5D%5BmaxCol%5D;%0A        // TODO%0A        return answer;%0A
+Math.min(maxRow - 1, row + K); r++) %7B%0A           
     
-%7D%0A%0A
     
-private static void print(int%5B%5D%5B%5D mat) %7B%0A        for (int%5B%5D m : mat) %7B%0A            System.out.println(Arrays.toString(m));%0A   
+ sum += (col + K %3E= maxCol ? sums%5Br%5D%5BmaxCol - 1%5D : sums%5Br%5D%5Bcol + K%5D) -%0A
      
-%7D%0A
     
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1314 prob = new Problem1314();%0A        print(prob.matrixBlockSum(new int%5B%5D%5B%5D%7B%0A            %7B1,2,3%7D,%0A            %7B4,5,6%7D,%0A            %7B7,8,9%7D%0A        %7D, 1)); // %5B%5B12,21,16%5D,%5B27,45,33%5D,%5B24,39,28%5D%5D%0A//        print(prob.matrixBlockSum(new int%5B%5D%5B%5D%7B%0A//            %7B1,2,3%7D,%0A//            %7B4,5,6%7D,%0A//            %7B7,8,9%7D%0A//        %7D, 2)); // %5B%5B45,45,45%5D,%5B45,45,45%5D,%5B45,45,45%5D%5D
+           (col - K - 1 %3C 0 ? 0 : sums%5Br%5D%5Bcol - K - 1%5D);%0A                %7D%0A                answer%5Brow%5D%5Bcol%5D = sum;%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1314