forked from super30admin/Array-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotproductofvectors.py
More file actions
62 lines (49 loc) · 1.71 KB
/
dotproductofvectors.py
File metadata and controls
62 lines (49 loc) · 1.71 KB
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
class SparseVector:
"""
Time complexity:
O(n) for creating the <index, value> pair for non-zero values; O(L+L 2 ) for calculating the dot product.
Space complexity:
O(L) for creating the <index, value> pairs for non-zero values. O(1) for calculating the dot product.
"""
def __init__(self, nums: List[int]):
self.array = []
for index, item in enumerate(nums):
if item != 0:
self.array.append([index, item])
# Return the dotProduct of two sparse vectors
def dotProduct(self, vec: 'SparseVector') -> int:
result = 0
p = 0
q = 0
while p < len(self.array) and q < len(vec.array):
###if indexes equal
if self.array[p][0] == vec.array[q][0]:
result += self.array[p][1] * vec.array[q][1]
p += 1
q += 1
elif self.array[p][0] < vec.array[q][0]:
p += 1
else:
q += 1
return result
# result=0
# for num1, num2 in zip(self.array, vec.array):
# result+=num1*num2
# return result
class SparseVector:
""" this approach is better
ON for constructing the sparse vector and On for calculating the dot product
space o1 below
"""
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)