Skip to content

Commit 5fefe1d

Browse files
Leetcode accepted but inefficient.
1 parent 6a34731 commit 5fefe1d

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dot Product of Two Sparse Vectors
2+
# https://leetcode.com/problems/dot-product-of-two-sparse-vectors/
3+
4+
class SparseVector:
5+
def __init__(self, nums):
6+
self.nums = nums
7+
"""
8+
:type nums: List[int]
9+
"""
10+
11+
12+
# Return the dotProduct of two sparse vectors
13+
def dotProduct(self, vec):
14+
if (len(vec.nums) != len(self.nums)):
15+
return []
16+
output = 0
17+
for index in range(len(self.nums)):
18+
output = output + (self.nums[index] * vec.nums[index])
19+
return output
20+
"""
21+
:type vec: 'SparseVector'
22+
:rtype: int
23+
"""
24+
25+
26+
# Your SparseVector object will be instantiated and called as such:
27+
# v1 = SparseVector(nums1)
28+
# v2 = SparseVector(nums2)
29+
# ans = v1.dotProduct(v2)

0 commit comments

Comments
 (0)