← 返回 meta 的题目列表Optimized Storage of Vectors with Duplicated Values
类型:online_judge
Given two vectors that may contain duplicate values, design a data structure to store such vectors in a space-efficient manner and efficiently compute the dot product of the two vectors.
Requirements
Describe your data structure design.
Implement a function dot_product(self, vector) to calculate the dot product of the current vector with vector.
Ensure the implementation is efficient for frequent calls.
Input
The vector is initialized with an integer array.
dot_product function receives another integer array as input.
Output
Return the integer value of the dot product.
Constraints
The length of vectors is n where 1 <= n <= 100,000.
The absolute value of vector elements is no greater than 10^4.
Test cases
Test case:
Input: vector1 = [1, 2, 0, 3, 0, 4], vector2 = [2, 1, 0, 3, 0, 0]
Output: 11
Input: vector1 = [0, 0, 0, 0, 3], vector2 = [0, 0, 0, 7, 5]
Output: 15
Example
Input
6
1 2 0 3 0 4
6
2 1 0 3 0 0