mediumArraysPattern: General

Top Scoring Entities Solution

Problem Statement

You are given an array of scores, where each score is associated with an entity. Return the top 5 entities with the highest scores. If there are more than 5 entities with the same score as the fifth highest score, include only the first 5 entities with that score.

Examples

Example 1:
Input:[["Alice", 3.8], ["Bob", 3.9], ["Charlie", 3.7], ["David", 3.9], ["Eve", 3.8], ["Frank", 3.9], ["George", 3.7]]
Output:[["Bob", 3.9], ["David", 3.9], ["Frank", 3.9], ["Alice", 3.8], ["Eve", 3.8]]
Explanation: Sorting students by GPA in descending order and selecting the top 5 students

Constraints

  • The input list will contain at least 5 students.
  • The GPA of each student will be a decimal number between 0 and 4.
  • The function should return a list of lists, where each sublist contains the name and GPA of a student.
  • If there are more than 5 students with the same GPA as the fifth highest GPA, include only the first 5 students with that GPA.
Time: O(n log n) Space: O(n)
The optimized approach involves using a more efficient sorting algorithm like quicksort or mergesort to sort the list of students. This approach has a time complexity of O(n log n) and is more suitable for large lists. Alternatively, a heap data structure can be used to find the top 5 students with the highest GPAs in O(n log k) time complexity.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.