LeetCode 1086: High Five Solution

Master LeetCode problem 1086 (High Five), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we need to find the average of the top five scores for each student. We can achieve this by sorting the scores for each student and then taking the average of the top five scores.

  1. Create a map to store the scores for each student.
  2. Iterate through the given list of scores and populate the map with the student id as the key and a list of scores as the value.
  3. For each student, sort their scores in descending order.
  4. Calculate the sum of the top five scores and divide by 5 to get the average.
  5. Return the averages in ascending order of student id.

Time Complexity: O(n*log(n)) where n is the total number of scores. Space Complexity: O(n) for the map.

: :

Solution Code

class Solution {
    public int[][] highFive(int[][] items) {
        Map<Integer, List<Integer>> scoresMap = new HashMap<>();
        
        for (int[] item : items) {
            int id = item[0];
            int score = item[1];
            
            if (!scoresMap.containsKey(id)) {
                scoresMap.put(id, new ArrayList<>());
            }
            scoresMap.get(id).add(score);
        }
        
        List<int[]> result = new ArrayList<>();
        for (Map.Entry<Integer, List<Integer>> entry : scoresMap.entrySet()) {
            int id = entry.getKey();
            List<Integer> scores = entry.getValue();
            Collections.sort(scores, Collections.reverseOrder());
            
            int sum = 0;
            for (int i = 0; i < 5; i++) {
                sum += scores.get(i);
            }
            
            result.add(new int[]{id, sum / 5});
        }
        
        return result.toArray(new int[0][]);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1086 (High Five)?

This page provides optimized solutions for LeetCode problem 1086 (High Five) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1086 (High Five)?

The time complexity for LeetCode 1086 (High Five) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1086 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1086 in Java, C++, or Python.

Back to LeetCode Solutions