Ctrl K

Multiply Mostly-Zero Matrices

Medium
You are given two integer matrices: left of size m×k and right of size k×n. Most of their entries are zero. Return the product left · right: an m×n matrix whose entry in row i and column j is the sum of left[i][t] · right[t][j] over all t. The sizes are guaranteed to match. Try not to waste time multiplying by zero.
Example 1
left=[[0, 2, 0], [0, 0, -1]], right=[[5, 0], [0, 3], [4, 0]] → [[0, 6], [-4, 0]]

Row [0, 2, 0] gives 2 · [0, 3] = [0, 6], and row [0, 0, -1] gives -1 · [4, 0] = [-4, 0].

Example 2
left=[[3]], right=[[0, -2, 7]] → [[0, -6, 21]]

A 1×1 matrix times a row: every entry is multiplied by 3.

Constraints
  • 1 ≤ m, k, n ≤ 100
  • -100 ≤ left[i][j], right[i][j] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumZero Out the Crosses MediumLonely Dark Pixels MediumDark Pixels in Twin Rows MediumMagic 3×3 Blocks Medium
loading Python…
Input
left=[[0, 2, 0], [0, 0, -1]], right=[[5, 0], [0, 3], [4, 0]]
Expected
[[0, 6], [-4, 0]]

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.