forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1570.java
26 lines (23 loc) · 863 Bytes
/
_1570.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.fishercoder.solutions;
public class _1570 {
public static class Solution1 {
/**This is a brute force but accepted solution.
* More optimal solution:
* use a map to store only non-zero values and use the smaller vector to do multiplication to reduce space and save time.*/
class SparseVector {
int[] vector;
SparseVector(int[] nums) {
this.vector = nums;
}
// Return the dotProduct of two sparse vectors
public int dotProduct(SparseVector vec) {
int[] incoming = vec.vector;
int dotProduct = 0;
for (int i = 0; i < vector.length; i++) {
dotProduct += incoming[i] * this.vector[i];
}
return dotProduct;
}
}
}
}