Skip to main content
Back to problems
#1086
Easy Algorithms

High five

Array Hash Table Sorting Heap (Priority Queue)
74.2% acceptance
Mar 31, 2026
829
131
Given a list of the scores of different students, items, where items[i] = [IDi, scorei] represents one score from a student with IDi, calculate each student's top five average. Return the answer as an array of pairs result, where result[j] = [IDj, topFiveAveragej] represents the student with IDj and their top five average. Sort result by IDj in increasing order. A student's top five average is calculated by taking the sum of their top five scores and dividing it by 5 using integer division.

Solution

Rust
Time O(n log n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn high_five(items: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
    let mut map = std::collections::HashMap::<i32, Vec<i32>>::new();
    for item in &items {
      map.entry(item[0]).or_default().push(item[1]);
    }
    let mut result: Vec<Vec<i32>> = map.into_iter().map(|(id, mut scores)| {
      scores.sort_unstable_by(|a, b| b.cmp(a));
      let avg = scores[..5].iter().sum::<i32>() / 5;
      vec![id, avg]
    }).collect();
    result.sort_by_key(|v| v[0]);
    result
  }
}