Difference Between Ones and Zeros in Row and Column
You are given a 0-indexed m x n binary matrix grid.
A 0-indexed m x n difference matrix diff is created with the following procedure:
- Let the number of ones in the
i^throw beonesRowi. - Let the number of ones in the
j^thcolumn beonesColj. - Let the number of zeros in the
i^throw bezerosRowi. - Let the number of zeros in the
j^thcolumn bezerosColj. diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
Return the difference matrix diff.
Example 1
0 1 1 1 0 1 0 0 1
Input
grid = [[0,1,1],[1,0,1],[0,0,1]]Output
[[0,0,4],[0,0,4],[-2,-2,2]]Each cell is computed as the number of ones in its row and column minus the number of zeros in its row and column.
Example 2
1 1 1 1 1 1
Input
grid = [[1,1,1],[1,1,1]]Output
[[5,5,5],[5,5,5]]Every row and column contains only ones, so each cell has value 3 + 2 - 0 - 0 = 5.
Constraints
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 10^5
- 1 <= m * n <= 10^5
- grid[i][j] is either 0 or 1.