返回介绍

solution / 1000-1099 / 1086.High Five / README_EN

发布于 2024-06-17 01:03:31 字数 5219 浏览 0 评论 0 收藏 0

1086. High Five

中文文档

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.

 

Example 1:

Input: items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
Output: [[1,87],[2,88]]
Explanation: 
The student with ID = 1 got scores 91, 92, 60, 65, 87, and 100. Their top five average is (100 + 92 + 91 + 87 + 65) / 5 = 87.
The student with ID = 2 got scores 93, 97, 77, 100, and 76. Their top five average is (100 + 97 + 93 + 77 + 76) / 5 = 88.6, but with integer division their average converts to 88.

Example 2:

Input: items = [[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100]]
Output: [[1,100],[7,100]]

 

Constraints:

  • 1 <= items.length <= 1000
  • items[i].length == 2
  • 1 <= IDi <= 1000
  • 0 <= scorei <= 100
  • For each IDi, there will be at least five scores.

Solutions

Solution 1

class Solution:
  def highFive(self, items: List[List[int]]) -> List[List[int]]:
    d = defaultdict(list)
    m = 0
    for i, x in items:
      d[i].append(x)
      m = max(m, i)
    ans = []
    for i in range(1, m + 1):
      if xs := d[i]:
        avg = sum(nlargest(5, xs)) // 5
        ans.append([i, avg])
    return ans
class Solution {
  public int[][] highFive(int[][] items) {
    int size = 0;
    PriorityQueue[] s = new PriorityQueue[101];
    int n = 5;
    for (int[] item : items) {
      int i = item[0], score = item[1];
      if (s[i] == null) {
        ++size;
        s[i] = new PriorityQueue<>(n);
      }
      s[i].offer(score);
      if (s[i].size() > n) {
        s[i].poll();
      }
    }
    int[][] res = new int[size][2];
    int j = 0;
    for (int i = 0; i < 101; ++i) {
      if (s[i] == null) {
        continue;
      }
      int avg = sum(s[i]) / n;
      res[j][0] = i;
      res[j++][1] = avg;
    }
    return res;
  }

  private int sum(PriorityQueue<Integer> q) {
    int s = 0;
    while (!q.isEmpty()) {
      s += q.poll();
    }
    return s;
  }
}
class Solution {
public:
  vector<vector<int>> highFive(vector<vector<int>>& items) {
    vector<int> d[1001];
    for (auto& item : items) {
      int i = item[0], x = item[1];
      d[i].push_back(x);
    }
    vector<vector<int>> ans;
    for (int i = 1; i <= 1000; ++i) {
      if (!d[i].empty()) {
        sort(d[i].begin(), d[i].end(), greater<int>());
        int s = 0;
        for (int j = 0; j < 5; ++j) {
          s += d[i][j];
        }
        ans.push_back({i, s / 5});
      }
    }
    return ans;
  }
};
func highFive(items [][]int) (ans [][]int) {
  d := make([][]int, 1001)
  for _, item := range items {
    i, x := item[0], item[1]
    d[i] = append(d[i], x)
  }
  for i := 1; i <= 1000; i++ {
    if len(d[i]) > 0 {
      sort.Ints(d[i])
      s := 0
      for j := len(d[i]) - 1; j >= len(d[i])-5; j-- {
        s += d[i][j]
      }
      ans = append(ans, []int{i, s / 5})
    }
  }
  return ans
}
function highFive(items: number[][]): number[][] {
  const d: number[][] = Array(1001)
    .fill(0)
    .map(() => Array(0));
  for (const [i, x] of items) {
    d[i].push(x);
  }
  const ans: number[][] = [];
  for (let i = 1; i <= 1000; ++i) {
    if (d[i].length > 0) {
      d[i].sort((a, b) => b - a);
      const s = d[i].slice(0, 5).reduce((a, b) => a + b);
      ans.push([i, Math.floor(s / 5)]);
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文