-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparse Matrix Multiplication.py
70 lines (53 loc) · 1.52 KB
/
Sparse Matrix Multiplication.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'''
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:
Input:
A = [
[ 1, 0, 0],
[-1, 0, 3]
]
B = [
[ 7, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 1 ]
]
Output:
| 1 0 0 | | 7 0 0 | | 7 0 0 |
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
| 0 0 1 |
'''
class Solution(object):
def multiply(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
row_a = len(A)
col_a = len(A[0])
row_b = len(B)
col_b = len(B[0])
res = [[0 for j in xrange(col_b)] for i in xrange(row_a)]
ka = {}
kb = {}
for i in xrange(row_a):
for j in xrange(col_a):
if A[i][j] != 0:
if j not in ka:
ka[j] = [(i, A[i][j])]
else:
ka[j].append((i, A[i][j]))
for i in xrange(row_b):
for j in xrange(col_b):
if B[i][j] != 0:
if i not in kb:
kb[i] = [(j, B[i][j])]
else:
kb[i].append((j, B[i][j]))
for k in ka:
if k in kb:
for (row, val_a) in ka[k]:
for (col, val_b) in kb[k]:
res[row][col] += val_a * val_b
return res