forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductRate.java
More file actions
44 lines (36 loc) · 1.11 KB
/
ProductRate.java
File metadata and controls
44 lines (36 loc) · 1.11 KB
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
import java.util.*;
public class ProductRate {
static Map<Integer, Double> helper(List<Result> rates, int k) {
Map<Integer, PriorityQueue<Double> > map = new HashMap<> ();
Map<Integer, Double> re = new HashMap<> ();
for(Result res: rates) {
int id = res.id;
double rt = res.rate;
map.putIfAbsent(id, new PriorityQueue<Double> ());
map.get(id).offer(rt);
if(map.get(id).size() > k) {
map.get(id).poll();
}
}
for(int id: map.keySet()) {
PriorityQueue<Double> scores = map.get(id);
int sz = scores.size();
double tot = 0.0;
for(double score: scores) tot += score;
re.put(id, tot / (sz * 1.0));
}
return re;
}
public static void main(String[] args) {
List<Result> rates = new ArrayList<> ();
rates.add(new Result(1, 10.0));
}
static class Result {
int id;
double rate;
public Result(int uid, double r) {
id = uid;
rate = r;
}
}
}