/
yelpDotProduct.java
52 lines (47 loc) · 1.41 KB
/
yelpDotProduct.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.HashMap;
import java.util.Scanner;
/**
* Created by yiyangtan on 9/8/15.
* ==============
4. dot product
==============
编程题就是给两个sparse vector,然后求dot product.
Given two sparse Vectors, compute the Dot Product.
Input Format : The first line will contain two numbers(k and n), which are the number of entries for the two vectors respectively.
The next k lines are the entries for the first vector, of the form : x y
where x is the position and y is the value at that position in the vector.
The n lines are the entries of the second vector.
Any entries not specified indicate zero at that position.
The two vectors will always be of the same length
Example input:
3 3
1 4
4 2
5 3
1 7
2 6
5 1
Sample Answer: Dot Product = 4*7+3*1 = 31 (only print 31)
*/
public class yelpDotProduct {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int n = sc.nextInt();
int result = 0;
HashMap<Integer, Integer> map = new HashMap<>();
while(k!=0){
map.put(sc.nextInt(),sc.nextInt());
k--;
}
while(n!=0){
int temp = sc.nextInt();
int temp2 = sc.nextInt();
if(map.containsKey(temp)){
result += map.get(temp)*temp2;
}
n--;
}
System.out.println(result);
}
}