-
Notifications
You must be signed in to change notification settings - Fork 0
/
1570-Dot_Product_of_Two_Sparse_Vectors.py
42 lines (33 loc) · 1.27 KB
/
1570-Dot_Product_of_Two_Sparse_Vectors.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
## Hash map
# n be the length of the input array and L be the number of non-zero elements.
# Time: O(n) for creating the Hash Map; O(L) for calculating the dot product.
# Space: O(1)
class SparseVector:
def __init__(self, nums: List[int]):
self.nonzeros = {}
for i, value in enumerate(nums):
if value:
self.nonzeros[i] = value
# Return the dotProduct of two sparse vectors
def dotProduct(self, vec: 'SparseVector') -> int:
result = 0
for num in self.nonzeros:
if num in vec.nonzeros:
result += self.nonzeros[num]*vec.nonzeros[num]
return result
## Non-efficient Array Approach
# Time: O(n) for both constructing the sparse vector and calculating the dot product.
# Space: O(1)
class SparseVector:
def __init__(self, nums: List[int]):
self.array = nums
# Return the dotProduct of two sparse vectors
def dotProduct(self, vec: 'SparseVector') -> int:
result = 0
for num1, num2 in zip(self.array, vec.array):
result += num1*num2
return result
# Your SparseVector object will be instantiated and called as such:
# v1 = SparseVector(nums1)
# v2 = SparseVector(nums2)
# ans = v1.dotProduct(v2)