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^th row be onesRowi.
  • Let the number of ones in the j^th column be onesColj.
  • Let the number of zeros in the i^th row be zerosRowi.
  • Let the number of zeros in the j^th column be zerosColj.
  • diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj

Return the difference matrix diff.

Example 1
0 1 1
1 0 1
0 0 1
Inputgrid = [[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
Inputgrid = [[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.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate