|
| 1 | +""" |
| 2 | +Problem Link: https://leetcode.com/problems/dot-product-of-two-sparse-vectors/ |
| 3 | +
|
| 4 | +Given two sparse vectors, compute their dot product. |
| 5 | +Implement class SparseVector: |
| 6 | +SparseVector(nums) Initializes the object with the vector nums |
| 7 | +dotProduct(vec) Compute the dot product between the instance of SparseVector and vec |
| 8 | +A sparse vector is a vector that has mostly zero values, you should store the sparse |
| 9 | +vector efficiently and compute the dot product between two SparseVector. |
| 10 | +Follow up: What if only one of the vectors is sparse? |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +Input: nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0] |
| 14 | +Output: 8 |
| 15 | +Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) |
| 16 | +v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8 |
| 17 | +
|
| 18 | +Example 2: |
| 19 | +Input: nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2] |
| 20 | +Output: 0 |
| 21 | +Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) |
| 22 | +v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0 |
| 23 | +
|
| 24 | +Example 3: |
| 25 | +Input: nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4] |
| 26 | +Output: 6 |
| 27 | +
|
| 28 | +Constraints: |
| 29 | +n == nums1.length == nums2.length |
| 30 | +1 <= n <= 10^5 |
| 31 | +0 <= nums1[i], nums2[i] <= 100 |
| 32 | +""" |
| 33 | +class SparseVector: |
| 34 | + def __init__(self, nums: List[int]): |
| 35 | + self.non_zeroes = [] |
| 36 | + for index, num in enumerate(nums): |
| 37 | + if num != 0: |
| 38 | + self.non_zeroes.append([index, num]) |
| 39 | + |
| 40 | + # Return the dotProduct of two sparse vectors |
| 41 | + def dotProduct(self, vec: 'SparseVector') -> int: |
| 42 | + prod = 0 |
| 43 | + index1 = index2 = 0 |
| 44 | + |
| 45 | + while index1 < len(self.non_zeroes) and index2 < len(vec.non_zeroes): |
| 46 | + if self.non_zeroes[index1][0] == vec.non_zeroes[index2][0]: |
| 47 | + prod += (self.non_zeroes[index1][1] * vec.non_zeroes[index2][1]) |
| 48 | + index1 += 1 |
| 49 | + index2 += 1 |
| 50 | + elif self.non_zeroes[index1][0] < vec.non_zeroes[index2][0]: |
| 51 | + index1 += 1 |
| 52 | + else: |
| 53 | + index2 += 1 |
| 54 | + |
| 55 | + return prod |
| 56 | + |
| 57 | + |
| 58 | +class SparseVector1: |
| 59 | + def __init__(self, nums: List[int]): |
| 60 | + self.non_zeroes = {} |
| 61 | + for index, num in enumerate(nums): |
| 62 | + if num != 0: |
| 63 | + self.non_zeroes[index] = num |
| 64 | + |
| 65 | + # Return the dotProduct of two sparse vectors |
| 66 | + def dotProduct(self, vec: 'SparseVector') -> int: |
| 67 | + prod = 0 |
| 68 | + for index, num in self.non_zeroes.items(): |
| 69 | + if index in vec.non_zeroes: |
| 70 | + prod += (vec.non_zeroes[index] * num) |
| 71 | + |
| 72 | + return prod |
| 73 | + |
| 74 | + |
| 75 | +# Your SparseVector object will be instantiated and called as such: |
| 76 | +# v1 = SparseVector(nums1) |
| 77 | +# v2 = SparseVector(nums2) |
| 78 | +# ans = v1.dotProduct(v2) |
0 commit comments