Find the dot product of two vectors [leetcode easy]

Leetcode

Example,

Input:
A: [1, 1, 4, 4, 4, 4, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
B: [2, 2, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Output: 291
Explanation: 1 2 + 1 2 + 4 5 + … + 2 3

Solution 1: Use a while loop to iterate through both arrays, add the product of two values at the same index to a running sum

time complexiity: O(min(n,m))
space complexity: O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

def dotProduct(arrA, arrB):
i = j = 0
maxSum = 0

while i < len(arrA) and j < len(arrB):
maxSum += arrA[i] * arrB[j]
i += 1
j += 1

return maxSum


A = [1, 1, 4, 4, 4, 4, 7, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
B = [2, 2, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

print(dotProduct(A, B))

Easy question,

is da best.