Difficulty: 🟡 Medium
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
ith
row beonesRowi
. - Let the number of ones in the
jth
column beonesColj
. - Let the number of zeros in the
ith
row bezerosRowi
. - Let the number of zeros in the
jth
column bezerosColj
. diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj
Return the difference matrix diff
.
Example 1:
Input: grid = [[0,1,1],[1,0,1],[0,0,1]]
Output: [[0,0,4],[0,0,4],[-2,-2,2]]
Explanation:
- diff[0][0] =onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 2 + 1 - 1 - 2 = 0
- diff[0][1] =onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 2 + 1 - 1 - 2 = 0
- diff[0][2] =onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 2 + 3 - 1 - 0 = 4
- diff[1][0] =onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 2 + 1 - 1 - 2 = 0
- diff[1][1] =onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 2 + 1 - 1 - 2 = 0
- diff[1][2] =onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 2 + 3 - 1 - 0 = 4
- diff[2][0] =onesRow2 + onesCol0 - zerosRow2 - zerosCol0 = 1 + 1 - 2 - 2 = -2
- diff[2][1] =onesRow2 + onesCol1 - zerosRow2 - zerosCol1 = 1 + 1 - 2 - 2 = -2
- diff[2][2] =onesRow2 + onesCol2 - zerosRow2 - zerosCol2 = 1 + 3 - 2 - 0 = 2
Example 2:
Input: grid = [[1,1,1],[1,1,1]]
Output: [[5,5,5],[5,5,5]]
Explanation:
- diff[0][0] = onesRow0 + onesCol0 - zerosRow0 - zerosCol0 = 3 + 2 - 0 - 0 = 5
- diff[0][1] = onesRow0 + onesCol1 - zerosRow0 - zerosCol1 = 3 + 2 - 0 - 0 = 5
- diff[0][2] = onesRow0 + onesCol2 - zerosRow0 - zerosCol2 = 3 + 2 - 0 - 0 = 5
- diff[1][0] = onesRow1 + onesCol0 - zerosRow1 - zerosCol0 = 3 + 2 - 0 - 0 = 5
- diff[1][1] = onesRow1 + onesCol1 - zerosRow1 - zerosCol1 = 3 + 2 - 0 - 0 = 5
- diff[1][2] = onesRow1 + onesCol2 - zerosRow1 - zerosCol2 = 3 + 2 - 0 - 0 = 5
m == grid.length
n == grid[i].length
1 <= m, n <= 105
1 <= m * n <= 105
grid[i][j]
is either0
or1
.
class Solution:
def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:
m, n = len(grid), len(grid[0])
cols, rows = {}, {}
for i in range(m):
rows[i] = {0: grid[i].count(0), 1: grid[i].count(1)}
for j in range(n):
cols[j] = {0: 0, 1: 0}
for i in range(m):
cols[j][0] += 1 if grid[i][j] == 0 else 0
cols[j][1] += 1 if grid[i][j] == 1 else 0
matrix = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
matrix[i][j] = rows[i][1] + cols[j][1] - rows[i][0] - cols[j][0]
return matrix
The given solution constructs the difference matrix, diff
, by calculating the differences between the counts of ones and zeros in each row and each column of the grid
. It uses dictionaries to store the counts of ones and zeros for rows and columns.
The algorithm works as follows:
- Initialize the dimensions of the
grid
as m (number of rows) and n (number of columns). - Initialize empty dictionaries,
cols
androws
, to store the counts of ones and zeros for columns and rows, respectively. - Iterate through each row of the
grid
:- Calculate the count of zeros and ones in the current row and store them in the
rows
dictionary.
- Calculate the count of zeros and ones in the current row and store them in the
- Iterate through each column of the
grid
:- Initialize an empty dictionary,
cols[j]
, to store the counts of zeros and ones for the current column j. - Iterate through each row of the
grid
:- Update the counts of zeros and ones for the current column based on the value of the current element in the
grid
.
- Update the counts of zeros and ones for the current column based on the value of the current element in the
- Initialize an empty dictionary,
- Create an empty
matrix
with dimensions m x n. - Iterate through each element of the
matrix
:- Calculate the difference between the counts of ones and zeros for the corresponding row and column and assign it to the current element of the
matrix
.
- Calculate the difference between the counts of ones and zeros for the corresponding row and column and assign it to the current element of the
- Return the
matrix
, which represents the difference matrixdiff
.
The algorithm calculates the counts of ones and zeros for each row and column and constructs the difference matrix based on these counts.
The time complexity of this algorithm is O(m * n), where m is the number of rows and n is the number of columns in the grid
. The algorithm iterates through each element of the grid
and performs constant-time operations at each iteration.
The space complexity of the algorithm is O(m + n), as it uses additional space to store the rows
and cols
dictionaries, each with a maximum size of m or n, respectively. The matrix
also requires O(m * n) space.
The given solution constructs a difference matrix diff
by calculating the differences between the counts of ones and zeros in each row and each column of the binary matrix grid
. It has a time complexity of O(m * n) and a space complexity of O(m + n + m * n), making it an efficient solution for the problem at hand.
NB: If you want to get community points please suggest solutions in other languages as merge requests.