DEV Community

Discussion on: Solution: Number of Submatrices That Sum to Target

Collapse
 
rohithv07 profile image
Rohith V

My Code on this problem :

class Solution {
    public int numSubmatrixSumTarget(int[][] matrix, int target) {
        int count = 0;
        int line = matrix.length;
        int column = matrix[0].length + 1;
        int[][] sum = new int[line][column];

        for (int i = 0; i < sum.length; i++){
            for (int j = 1; j < sum[0].length; j++){
                sum[i][j] = sum[i][j - 1] + matrix[i][j - 1];
            }
        }


        for (int start = 0; start < column; start++){
            for (int end = start + 1; end < column; end++ ){

                int sumOfSubMatrix = 0;
                Map<Integer, Integer> map = new HashMap<Integer, Integer>();
                map.put(0, 1);
                for(int l = 0; l < line; l++){
                    sumOfSubMatrix += sum[l][end] - sum[l][start];
                    if (map.containsKey(sumOfSubMatrix - target))
                        count += map.get(sumOfSubMatrix - target);
                    map.put(sumOfSubMatrix, map.getOrDefault(sumOfSubMatrix, 0) + 1);

                }
            }
        }

        return count;

    }
}
Enter fullscreen mode Exit fullscreen mode

Repo : github.com/Rohithv07/LeetCodeTopIn...