Given two sparse vectors, compute their dot product.
A sparse vector is a vector that has mostly zero values.
Dot product of two vectors is the sum of their individual products.
For example, if we have vectors A and B of equal length n, their dot product is:
A * B = A[0] * B[0] + A[1] * B[1] + ... + A[n - 1] * B[n - 1]
We represent a sparse vector as a list of pairs (index, value), where value != 0.
E.g. the vector [6, 0, 0, 9, 7] will be given as [[0, 6], [3, 9], [4, 7]]
Example:
Input: first = [[0, 6], [3, 9], [4, 7]]
second = [[1, 3], [3, 4]]
Output: 36
Explanation:
[6, 0, 0, 9, 7] * [0, 3, 0, 4, 0] =
= 6 * 0 + 0 * 3 + 0 * 0 + 9 * 4 + 7 * 0 = 36
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.