LeetCode 1570. Dot Product of Two Sparse Vectors
原题链接在这里:https://leetcode.com/problems/dot-product-of-two-sparse-vectors/description/
题目:
Given two sparse vectors, compute their dot product.
Implement class SparseVector
:
SparseVector(nums)
Initializes the object with the vectornums
dotProduct(vec)
Compute the dot product between the instance of SparseVector andvec
A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector.
Follow up: What if only one of the vectors is sparse?
Example 1:
Input: nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0] Output: 8 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8
Example 2:
Input: nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2] Output: 0 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
Example 3:
Input: nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4] Output: 6
Constraints:
n == nums1.length == nums2.length
1 <= n <= 10^5
0 <= nums1[i], nums2[i] <= 100
题解:
Since the vector is sparce, it only make sense to store the indice of elements that are not 0.
We have a map to store the index, element if element != 0.
When multiply, go through the second vec's map and find if the corresponding index is in the first vec's map. If yes, then accumulate the results.
If only one of the vectors is sparse, taking advantage of map, we want to initialize with the non-sparce vector. And dotProduct with the sparce vector. Then j, n in vec.m.items() is a small map.
Time Complexity: constructor, O(m). m = nums.length. dotProduct, O(n). n = vec.length.
Space: O(m + n).
AC Java:
1 class SparseVector { 2 Map<Integer, Integer> hm; 3 4 SparseVector(int[] nums) { 5 hm = new HashMap<>(); 6 for(int i = 0; i < nums.length; i++){ 7 if(nums[i] != 0){ 8 hm.put(i, nums[i]); 9 } 10 } 11 } 12 13 // Return the dotProduct of two sparse vectors 14 public int dotProduct(SparseVector vec) { 15 if(this.hm.size() < vec.hm.size()){ 16 return vec.dotProduct(this); 17 } 18 19 int res = 0; 20 for(Map.Entry<Integer, Integer> entry : vec.hm.entrySet()){ 21 if(this.hm.containsKey(entry.getKey())){ 22 res += this.hm.get(entry.getKey()) * entry.getValue(); 23 } 24 } 25 26 return res; 27 } 28 } 29 30 // Your SparseVector object will be instantiated and called as such: 31 // SparseVector v1 = new SparseVector(nums1); 32 // SparseVector v2 = new SparseVector(nums2); 33 // int ans = v1.dotProduct(v2);
AC Python:
1 class SparseVector: 2 def __init__(self, nums: List[int]): 3 self.m = {} 4 for i, n in enumerate(nums): 5 if n != 0: 6 self.m[i] = n 7 8 # Return the dotProduct of two sparse vectors 9 def dotProduct(self, vec: 'SparseVector') -> int: 10 res = 0 11 for j, n in vec.m.items(): 12 if j in self.m: 13 res += self.m[j] * n 14 return res 15 16 # Your SparseVector object will be instantiated and called as such: 17 # v1 = SparseVector(nums1) 18 # v2 = SparseVector(nums2) 19 # ans = v1.dotProduct(v2)