Given two sparse matrices mat1 of size m x k and mat2 of size k x n, return the result of mat1 x mat2. You may assume that multiplication is always possible.
Example 1:
Input: mat1 = [[1,0,0],
[-1,0,3]],
mat2 = [[7,0,0],
[0,0,0],
[0,0,1]]
Output: [[7,0,0],
[-7,0,3]]Example 2:
Input: mat1 = [[0]], mat2 = [[0]]
Output: [[0]]Constraints:
m == mat1.lengthk == mat1[i].length == mat2.lengthn == mat2[i].length1 <= m, n, k <= 100-100 <= mat1[i][j], mat2[i][j] <= 100class Solution:
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
# Product matrix.
ans = [[0] * len(mat2[0]) for _ in range(len(mat1))]
for row_index, row_elements in enumerate(mat1):
for element_index, row_element in enumerate(row_elements):
# If current element of mat1 is non-zero then iterate over all columns of mat2.
if row_element:
for col_index, col_element in enumerate(mat2[element_index]):
ans[row_index][col_index] += row_element * col_element
return ansWhere and represent the number of rows and columns in
mat1, respectively and and represent the number of rows and columns inmat2, respectively.
class Solution:
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
def compress_matrix(matrix: List[List[int]]) -> List[List[int]]:
rows, cols = len(matrix), len(matrix[0])
compressed_matrix = [[] for _ in range(rows)]
for row in range(rows):
for col in range(cols):
if matrix[row][col]:
compressed_matrix[row].append([matrix[row][col], col])
return compressed_matrix
m = len(mat1)
k = len(mat1[0])
n = len(mat2[0])
# Store the non-zero values of each matrix.
A = compress_matrix(mat1)
B = compress_matrix(mat2)
ans = [[0] * n for _ in range(m)]
for mat1_row in range(m):
# Iterate on all current 'row' non-zero elements of mat1.
for element1, mat1_col in A[mat1_row]:
# Multiply and add all non-zero elements of mat2
# where the row is equal to col of current element of mat1.
for element2, mat2_col in B[mat1_col]:
ans[mat1_row][mat2_col] += element1 * element2
return ansWhere and represent the number of rows and columns in
mat1, respectively and and represent the number of rows and columns inmat2, respectively.
class SparseMatrix:
def __init__(self, matrix: List[List[int]], col_wise: bool):
self.values, self.row_index, self.col_index = self.compress_matrix(matrix, col_wise)
def compress_matrix(self, matrix: List[List[int]], col_wise: bool):
return self.compress_col_wise(matrix) if col_wise else self.compress_row_wise(matrix)
# Compressed Sparse Row
def compress_row_wise(self, matrix: List[List[int]]):
values = []
row_index = [0]
col_index = []
for row in range(len(matrix)):
for col in range(len(matrix[0])):
if matrix[row][col]:
values.append(matrix[row][col])
col_index.append(col)
row_index.append(len(values))
return values, row_index, col_index
# Compressed Sparse Column
def compress_col_wise(self, matrix: List[List[int]]):
values = []
row_index = []
col_index = [0]
for col in range(len(matrix[0])):
for row in range(len(matrix)):
if matrix[row][col]:
values.append(matrix[row][col])
row_index.append(row)
col_index.append(len(values))
return values, row_index, col_index
class Solution:
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
A = SparseMatrix(mat1, False)
B = SparseMatrix(mat2, True)
ans = [[0] * len(mat2[0]) for _ in range(len(mat1))]
for row in range(len(ans)):
for col in range(len(ans[0])):
# Row element range indices
mat1_row_start = A.row_index[row]
mat1_row_end = A.row_index[row + 1]
# Column element range indices
mat2_col_start = B.col_index[col]
mat2_col_end = B.col_index[col + 1]
# Iterate over both row and column.
while mat1_row_start < mat1_row_end and mat2_col_start < mat2_col_end:
if A.col_index[mat1_row_start] < B.row_index[mat2_col_start]:
mat1_row_start += 1
elif A.col_index[mat1_row_start] > B.row_index[mat2_col_start]:
mat2_col_start += 1
# Row index and col index are same so we can multiply these elements.
else:
ans[row][col] += A.values[mat1_row_start] * B.values[mat2_col_start]
mat1_row_start += 1
mat2_col_start += 1
return ansWhere and represent the number of rows and columns in
mat1, respectively and and represent the number of rows and columns inmat2, respectively.