← 返回 bytedance 的题目列表Dot Product of Two Sparse Vectors
类型:online_judge
Given two sparse vectors, compute their dot product.
The vectors A and B have the same length n, where most elements are zero. We store the non-zero elements of these two vectors in two lists. Each element in the list is in the form of (index, value), representing a non-zero value at that index in the vector.
Write a function to compute the dot product of the two vectors. The dot product is defined as the sum of A[i] * B[i].
Input Format:
Two integer arrays, representing the non-zero values of vectors A and B, respectively.
Output Format:
Integer, representing the result of the dot product.
Example 1:
Input: A = [(0, 1), (2, 3)], B = [(1, 4), (2, 5)] Output: 15
Output Explanation:
The dot product is computed as 3*5 = 15.
Constraints:
The number of non-zero elements does not exceed 1000.
Example
Input
(0, 1), (2, 3)
(1, 4), (2, 5)