1570. Dot Product of Two Sparse Vectors

1570. 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 vector nums
  • dotProduct(vec)Compute the dot product between the instance of SparseVector and vec

A sparse vector is a vector that has mostly zero values, you should store the sparse vectorefficiently and compute the dot product between two SparseVector.

Follow up: What if only one of the vectors is sparse?

Example 1:

1
2
3
4
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:

1
2
3
4
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:

1
2
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

Hints/Notes

Solution

Language: C++

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
class SparseVector {
public:
vector<pair<int, int>> vals;

SparseVector(vector<int> &nums) {
for (int i = 0; i < nums.size(); i++) {
if (nums[i]) {
vals.push_back({i, nums[i]});
}
}
}

// Return the dotProduct of two sparse vectors
int dotProduct(SparseVector& vec) {
int res = 0;
auto vals2 = vec.getValues();
int m = vals.size(), n = vals2.size(), i = 0, j = 0;
while (i < m && j < n) {
if (vals[i].first == vals2[j].first) {
res += vals[i++].second * vals2[j++].second;
} else if (vals[i].first > vals2[j].first) {
j++;
} else {
i++;
}
}
return res;
}

vector<pair<int, int>> getValues() {
return vals;
}
};

// Your SparseVector object will be instantiated and called as such:
// SparseVector v1(nums1);
// SparseVector v2(nums2);
// int ans = v1.dotProduct(v2);