Dot Product

Start Timer

0:00:00

Upvote
1
Downvote
Save question
Mark as completed
View comments

Given two vectors nums1 and nums2 of equal length, return their dot product.

The dot product of two vectors is defined as:

dot(nums1,nums2)=i=0n1nums1[i]×nums2[i] \text{dot}(nums1, nums2) = \sum_{i=0}^{n-1} nums1[i] \times nums2[i]

You must implement the solution without using built-in functions like numpy.dot.

Example 1

Input

nums1 = [1,2,3], nums2 = [4,5,6]
def dotProduct(nums1, nums2) -> 32

Explanation: 1*4 + 2*5 + 3*6 = 32

Example 2

Input

nums1 = [0,1,0], nums2 = [1,0,1]
def dotProduct(nums1, nums2) -> 0

Explanation: 0*1 + 1*0 + 0*1 = 0

Constraints

  • nums1 and nums2 are lists of integers of the same length n
  • 1 <= n <= 10^4
  • -100 <= nums1[i], nums2[i] <= 100
.
.
.
.
.


Comments

Loading comments