Amazon

High Five

There are two properties in the node student id and scores, to ensure that each student will have at least 5 points, find the average of 5 highest scores for each person.

/**
 * Definition for a Record
 * class Record {
 *     public int id, score;
 *     public Record(int id, int score){
 *         this.id = id;
 *         this.score = score;
 *     }
 * }
 */
public class Solution {
    public Map<Integer, Double> highFive(Record[] results) {
        Map<Integer, Double> ans = new HashMap<>();
        Map<Integer, PriorityQueue<Integer>> minHeapMap = new HashMap<>();

        for(Record r: results){
            if(!minHeapMap.containsKey(r.id)){
                minHeapMap.put(r.id, new PriorityQueue<>(5));
            }
            minHeapMap.get(r.id).add(r.score);
            if(minHeapMap.get(r.id).size() > 5){
                minHeapMap.get(r.id).poll();
            }
        }

        for(Map.Entry<Integer, PriorityQueue<Integer>> entry: minHeapMap.entrySet()){
            double sum = 0.0;
            while(!entry.getValue().isEmpty()){
                sum+=entry.getValue().poll();
            }
            ans.put(entry.getKey(), sum/5);
        }

        return ans;
    }
}

Maximum Minimum Path in Matrix

给一个矩阵,

先找所有从左上到右下的path. 找出每个path的最小值. 找出这些最小值中的最大值.

For Example: [[8,4,3,5] [6,5,9,8]]

所有的path:

8->4->3->5->8 min:3

8->4->3->9->8 min:3

8->4->5->9->8 min:5

8->6->5->9->8 min:5

Result = Math.max(3,3,5,5,) = 5

Last updated

Was this helpful?