311-sparse-matrix-multiplication
Question
https://leetcode.com/problems/sparse-matrix-multiplication/description/
Given two sparse matrices A and B, return the result of AB.
You may assume that A's column number is equal to B's row number.
Example:
A = [
[ 1, 0, 0],
[-1, 0, 3]
]
B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
| 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
Thought Process
- Dot Product (TLE)
- Follow the rule of dot product
- Time complexity O(mno), where m = rows of A, n = columns of A, o = columns of B
- Space complexity O(mo)
- Skip Value
- Instead of loop the rows of A and columns of B, we should loop through all the elements in the A then columns of B
- If the value of at particular i and j index is 0, we can skip jth row in B all together to save the work
- Time complexity O(mno), depends on the density of the graph
- Space complexity O(mo)
Solution
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if (A.length == 0 || B.length == 0) return new int[0][0];
int m = A.length, n = A[0].length, o = B[0].length;
int[][] res = new int[m][o];
for (int i = 0; i < m; i++) {
for (int j = 0; j < o; j++) {
for (int k = 0; k < n; k++) {
res[i][j] += A[i][k] * B[k][j];
}
}
}
return res;
}
}
class Solution {
public int[][] multiply(int[][] A, int[][] B) {
if (A.length == 0 || B.length == 0) return new int[0][0];
int m = A.length, n = A[0].length, o = B[0].length;
int[][] res = new int[m][o];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// if the value is not 0, it countribute to the sum
if (A[i][j] != 0) {
for (int k = 0; k < o; k++) {
if (B[j][k] != 0) res[i][k] += A[i][j] * B[j][k];
}
}
}
}
return res;
}
}