# Five score average

• Input is List<result> Where result is:
result {
int id;
int score;
}
Output should be a map.
The question is there are a list of students where each of student has at least five scores. We want to output a map where key is the student if and value is the average of studrtns's highest five scores
It should be done with PriorityQueue with overriding the compare but how to implement specifically?

• Java based solution: Create a Map<Integer, PriorityQueue<Integer>> scoreMap, parse the list and put each student's scores in their corresponding list. Then create a Map<Integer, Double> resultMap, iterate over scoreMap and for each key fetch top 5 scores, take the average, populate in resultMap and return resultMap.

``````public Map<Integer, Double> scoresAverage(List<Result> results){
Map<Integer, PriorityQueue<Integer>> scoresMap = new HashMap<Integer, PriorityQueue<Integer>>();
for(Result result : results){
if(scoresMap.containsKey(result.id)){
PriorityQueue<Integer> pq = scoresMap.get(result.id);
scoresMap.put(result.id, pq);
}else{
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Collections.reverseOrder());
scoresMap.put(result.id, pq);
}
}

Map<Integer, Double> resultMap = new HashMap<Integer, Double>();
for(Map.Entry<Integer, PriorityQueue<Integer>> entry : scoresMap.entrySet()){
PriorityQueue<Integer> pq = entry.getValue();
double average = getAverage(pq, 5);
resultMap.put(entry.getKey(), average);
}
return resultMap;
}

public double getAverage(PriorityQueue<Integer> pq, int number){
double total = 0.0;
int count = number;
while(count >0 && !pq.isEmpty()){
total = total + pq.remove();
count--;;
}
}

`````````

• A simple solution for this question as below:

``````public Map<Integer, Integer> average(List<Result> scores) {
Map<Integer, Integer> results = new HashMap<Integer, Integer>();

if( scores == null || scores.size() ==0) return results;

Map<Integer, PriorityQueue<Integer>> scoresMap = new HashMap<Integer, PriorityQueue<Integer>>();

PriorityQueue<Integer>  queue = null;
for(Result s: scores) {
queue = scoresMap.get(s.id);
if(queue == null) {
queue = new PriorityQueue<Integer>(Collections.<Integer>reverseOrder());
scoresMap.put(s.id, queue);
}
}

long avg=0;
for(Integer id: scoresMap.keySet()) {
queue = scoresMap.get(id);
for(int i=0; i<5; i++) {
avg+=queue.poll();
}
results.put(id, (int)(avg/5));
avg=0;
}

return results;
}
``````

Class Result :

``````   class Result{
int id;
int score;

public Result(){}
public Result(int id, int score) {
this.id = id;
this.score = score;
}
}
````````

• Here's a solution. Average can be `double` or `int` and this may be a good thing to bring up in an interview to highlight your attention to detail.

``````    class Result {
int id, score;
Result(int i, int s) { id = i; score = s; }
}

public Map<Integer, Integer> topFives(List<Result> results) {
Map<Integer, Queue<Result>> map = new HashMap<>();
for (Result r : results) {
Queue<Result> q = null;
if ((q = map.get(r.id)) == null)
map.put(r.id, q = new PriorityQueue<>((a, b) -> b.score - a.score));
q.offer(r);
}
Map<Integer, Integer> avgs = new HashMap<>();
for (Map.Entry<Integer, Queue<Result>> r : map.entrySet()) {
int avg = 0;
for (int i = 0; i < 5; i++) {
avg += r.getValue().poll().score;
}
avgs.put(r.getKey(), avg / 5);
}
return avgs;
}

@Test
public void testAboveCode() {
assertEquals(Collections.singletonMap(1, 76), topFives(Arrays.asList(new Result(1, 100), new Result(1, 80),
new Result(1, 90), new Result(1, 70), new Result(1, 40), new Result(1, 20))));
}
``````

• `````` static class Record {
public int id, score;
public Record(int id, int score){
this.id = id;
this.score = score;
}
}
public static Map<Integer, Double> highFive(Record[] results) {
HashMap<Integer, Double> hashmap = new HashMap<>();
HashMap<Integer, PriorityQueue<Integer>> priHashmap = new HashMap<Integer, PriorityQueue<Integer>>();
PriorityQueue<Integer> priQue;
for(Record result : results){
priQue = priHashmap.get(result.id);
if(!priHashmap.containsKey(result.id)){
priQue = new PriorityQueue<Integer>(5,(a,b)->b-a);
priQue.offer(result.score);
priHashmap.put(result.id, priQue);

} else {
}
}

for(Integer key: priHashmap.keySet()){
priQue = priHashmap.get(key);
System.out.println("key"+priQue);
Double sum = 0.0;
for(int i = 0; i < 5; i++){
sum += priQue.poll();
}
hashmap.put(key, sum/5);
}
return hashmap;
}``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.