forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sparse_Matrix_Multiplication.cpp
56 lines (55 loc) · 1.71 KB
/
Sparse_Matrix_Multiplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// optimized version. AC :)
class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
int row1 = A.size();
int col1 = A[0].size();
int row2 = B.size();
int col2 = B[0].size();
vector<vector<int>> product(row1, vector<int>(col2, 0));
for(int i = 0; i < row1; i++) {
bool zeroRow = true;
for(int p = 0; p < col1; ++p) {
if(A[i][p] != 0) {
zeroRow = false;
break;
}
}
if(zeroRow) continue;
for(int j = 0; j < col2; ++j) {
bool zeroCol = true;
for(int q = 0; q < row2; ++q) {
if(B[q][j] != 0) {
zeroCol = false;
break;
}
}
if(zeroCol) continue;
for(int k = 0; k < col1; ++k) {
if(A[i][k] == 0 or B[k][j] == 0) continue;
product[i][j] += (A[i][k] * B[k][j]);
}
}
}
return product;
}
};
// TLE
class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
int row1 = A.size();
int col1 = A[0].size();
int row2 = B.size();
int col2 = B[0].size();
vector<vector<int>> product(row1, vector<int>(col2, 0));
for(int i = 0; i < row1; i++) {
for(int j = 0; j < col2; ++j) {
for(int k = 0; k < col1; ++k) {
product[i][j] += (A[i][k] * B[k][j]);
}
}
}
return product;
}
};