...

/

Untitled Masterpiece

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

  • 1n1031 \leq n \leq 10^3

  • 00 \leq nums1[i], nums2[i] 100\leq 100

Examples

Press + to interact
canvasAnimation-image
1 / 3

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Dot Product of Two Sparse Vectors

1

What is the result of the dot product of the sparse vectors nums1 =[0,4,0,0,0,0,7]= [0,4,0,0,0,0,7] and nums2 =[3,0,0,0,0,5,0]= [3,0,0,0,0,5,0]?

A)

0

B)

12

C)

21

D)

15

Question 1 of 30 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground. 

Press + to interact
Python
usercode > sparse_vector.py
class SparseVector:
def __init__(self, nums):
# Write your code here
pass
def dot_product(self, vec):
# Write your code here
return -1
Dot Product of Two Sparse Vectors

Access this course and 1200+ top-rated courses and projects.