<filename>Leetcode/0311. Sparse Matrix Multiplication/0311-2.py
class Solution:
  def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
    m = len(mat1)
    n = len(mat2)
    l = len(mat2[0])
    ans = [[0] * l for _ in range(m)]
    nonZeroColIndicesInMat2 = [
        [j for j, a in enumerate(row) if a]
        for row in mat2
    ]

    for i in range(m):
      for j, a in enumerate(mat1[i]):
        if a == 0:
          continue
        # mat1's j-th column matches mat2's j-th row
        for colIndex in nonZeroColIndicesInMat2[j]:
          ans[i][colIndex] += a * mat2[j][colIndex]

    return ans

<filename>Leetcode/0311. Sparse Matrix Multiplication/0311-2.py
class Solution:
  def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
    m = len(mat1)
    n = len(mat2)
    l = len(mat2[0])
    ans = [[0] * l for _ in range(m)]
    nonZeroColIndicesInMat2 = [
        [j for j, a in enumerate(row) if a]
        for row in mat2
    ]

    for i in range(m):
      for j, a in enumerate(mat1[i]):
        if a == 0:
          continue
        # mat1's j-th column matches mat2's j-th row
        for colIndex in nonZeroColIndicesInMat2[j]:
          ans[i][colIndex] += a * mat2[j][colIndex]

    return ans

# mat1's j-th column matches mat2's j-th row