You are given two sparse arrays. Your task is to calculate their dot product efficiently.
Design a class named SparseArray with the following features:
SparseArray(arr): Initializes the object with the array arrcomputeDot(otherArray): Returns the dot product of the current SparseArray instance with another SparseArray instance otherArrayNote: A sparse array contains mostly zero values. You should store the sparse array in a way that uses memory efficiently and allows fast dot product computation.
Array1 = [2,0,0,1,4], Array2 = [0,5,0,3,0]
Result: 3
Explanation:
sa1 = SparseArray(Array1), sa2 = SparseArray(Array2)
sa1.computeDot(sa2) = 2*0 + 0*5 + 0*0 + 1*3 + 4*0 = 3
Array1 = [0,3,0,0,0], Array2 = [0,0,0,0,6]
Result: 0
Explanation:
sa1 = SparseArray(Array1), sa2 = SparseArray(Array2)
sa1.computeDot(sa2) = 0*0 + 3*0 + 0*0 + 0*0 + 0*6 = 0
Array1 = [0,2,0,0,1,0,0], Array2 = [4,0,0,0,5,0,7]
Result: 7
Explanation:
sa1 = SparseArray(Array1), sa2 = SparseArray(Array2)
sa1.computeDot(sa2) = 0*4 + 2*0 + 0*0 + 0*0 + 1*5 + 0*0 + 0*7 = 7