【leetcode】1074. Number of Submatrices That Sum to Target

Topics are as follows:

Given a matrix, and a target, return the number of non-empty submatrices that sum to target.

A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.

Two submatrices (x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.

 

Example 1:

Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.

Example 2:

Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.

 

Note:

  1. 1 <= matrix.length <= 300
  2. 1 <= matrix[0].length <= 300
  3. -1000 <= matrix[i] <= 1000
  4. -10^8 <= target <= 10^8

Outline of Solution: The time complexity of calculation is a method of violence O (n ^ 4), and the maximum is 300 matrix.length, should not be AC. That O (n ^ 3) can do? Try it. First, the introduction of a two-dimensional array val_grid, referred var_grid [m] [n] is a value from 0 to n-th column and m-th row in this range, for example, with an input of Example 1 [[0,1,0], [1,1,1], [0,1,0]], corresponding to val_grid [[0, 1, 0], [1, 2, 1], [1, 3, 1]]. To calculate how many equal and meet the target sub-matrix, the matrix is easy in the j-th line to the i-th row interval can be determined in this sub-matrix and each column, such as a column and the k-th to val_grid [i] [k] - val_grid [j- 1] [k] (j> 0), the next order k in range (0, len ( matrix [0]), sequentially determined [j ~ i] of each column is equal to target while accumulating and recording starts from column 0 and, assuming [0 ~ k] and columns are accumulated sum, as long as the history is determined and the number of columns that satisfy sum - target, to obtain [j ~ i] son the number of k sub-matrix as a matrix which satisfies a condition of the right-hand column so as to optimize the complexity of O (n ^ 3).

code show as below:

class Solution(object):
    def numSubmatrixSumTarget(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: int
        """
        val_grid = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
        for j in range(len(matrix[0])):
            amount = 0
            for i in range(len(matrix)):
                amount += matrix[i][j]
                val_grid[i][j] = amount
        #print val_grid

        res = 0
        for i in range(len(matrix)):
            for j in range(i+1):
                December = {}
                amount = 0
                for k in range(len(matrix[i])):
                    v = val_grid[i][k]
                    if j > 0:
                        to v - = valgrid [j-1 ] [k]
                    amount += v
                    if amount == target:res += 1
                    if amount - target in dic:
                        res += dic[amount - target]
                    dic[amount] = dic.setdefault(amount,0) + 1
        return res

 



 

Guess you like

Origin www.cnblogs.com/seyjs/p/11101755.html