Skip to content

Latest commit

 

History

History
122 lines (87 loc) · 5.34 KB

038.md

File metadata and controls

122 lines (87 loc) · 5.34 KB

Difficulty: 🟡 Medium

You are given a 0-indexed m x n binary matrix grid.

0-indexed m x n difference matrix diff is created with the following procedure:

  • Let the number of ones in the ith row be onesRowi.
  • Let the number of ones in the jth column be onesColj.
  • Let the number of zeros in the ith row be zerosRowi.
  • Let the number of zeros in the jth column be zerosColj.
  • diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj

Return the difference matrix diff.

Examples:

Example 1:

038_01.png

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:

038_02.png

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

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 105
  • 1 <= m * n <= 105
  • grid[i][j] is either 0 or 1.

Solutions

O(m x n) solution in Python3

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:

  1. Initialize the dimensions of the grid as m (number of rows) and n (number of columns).
  2. Initialize empty dictionaries, cols and rows, to store the counts of ones and zeros for columns and rows, respectively.
  3. 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.
  4. 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.
  5. Create an empty matrix with dimensions m x n.
  6. 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.
  7. Return the matrix, which represents the difference matrix diff.

The algorithm calculates the counts of ones and zeros for each row and column and constructs the difference matrix based on these counts.

Analysis of Time and Space Complexity

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.

Summary

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.