Dot Product of Two Sparse Vectors

Try to solve the Dot Product of Two Sparse Vectors problem.

Statement

We must calculate the dot product of two given sparse vectors, nums1 and nums2.

Create a SparseVector class:

  • Constructor(): Initializes the object with the vector.

  • DotProduct(): Computes the dot product between the current instance of the vector and the other.

Note: A sparse vector is a vector that contains mostly zero values. Therefore, we should store the sparse vectors and calculate the dot product accordingly.

Constraints:

  • n==n == nums1.length ==== nums2.length

  • 1<=n<=1031 <= n <= 10^3

  • 0<=0 <= nums1[i], nums2[i] <=100<= 100

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.