1570. Dot Product of Two Sparse Vectors
On This Page
class SparseVector:
def __init__(self, nums: List[int]):
self.d = {i: v for i, v in enumerate(nums) if v}
# Return the dotProduct of two sparse vectors
def dotProduct(self, vec: "SparseVector") -> int:
a, b = self.d, vec.d
if len(b) < len(a):
a, b = b, a
return sum(v * b.get(i, 0) for i, v in a.items())
# Your SparseVector object will be instantiated and called as such:
# v1 = SparseVector(nums1)
# v2 = SparseVector(nums2)
# res = v1.dotProduct(v2)