GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1086 - High Five - LeetCode JavaScript Solutions

Back to all solutions

#1086 - High Five

Easy • View on LeetCode • View on GitHub
Array Hash Table Sorting Heap Priority Queue

Problem Description

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

/**
 * @param {number[][]} items
 * @return {number[][]}
 */
var highFive = function(items) {
  const studentScores = new Map();

  for (const [id, score] of items) {
    if (!studentScores.has(id)) {
      studentScores.set(id, []);
    }
    studentScores.get(id).push(score);
  }

  const result = [];
  for (const [id, scores] of studentScores) {
    scores.sort((a, b) => b - a);
    const topFiveSum = scores.slice(0, 5).reduce((sum, score) => sum + score, 0);
    const average = Math.floor(topFiveSum / 5);
    result.push([id, average]);
  }

  result.sort((a, b) => a[0] - b[0]);

  return result;
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.