题目地址:https://leetcode-cn.com/problems/high-five/

题目描述

Given a list of scores of different students, return the average score of each student’s top five scores in the order of each student’s id.

Each entry items[i] has items[i][0] the student’s id, and items[i][1] the student’s score. The average score is calculated using integer division.

Example 1:

Input: [[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 average of the student with id = 1 is 87.
The average of the student with id = 2 is 88.6. But with integer division their average converts to 88.

Note:

  1. 1 <= items.length <= 1000
  2. items[i].length == 2
  3. The IDs of the students is between 1 to 1000
  4. The score of the students is between 1 to 100
  5. For each student, there are at least 5 scores

题目大意

给你一个不同学生的分数列表,请按 学生的 id 顺序 返回每个学生 最高的五科 成绩的 平均分。
对于每条 items[i] 记录, items[i][0] 为学生的 id,itemsi 为学生的分数。平均分请采用整数除法计算。

解题方法

大根堆

给每个学生一个大根堆,把其所有的成绩都放入堆中,最后堆中最大的5个数字就是最高的5科成绩。

C++代码如下:

class Solution {
public:
vector<vector<int>> highFive(vector<vector<int>>& items) {
vector<priority_queue<int>> scores(1010);
for (auto& item : items) {
scores[item[0]].push(item[1]);
}
vector<vector<int>> res;
for (int i = 0; i < scores.size(); ++i) {
auto& queue = scores[i];
if (queue.empty()) continue;
int sum = 0;
for (int j = 0; j < 5; ++j) {
sum += queue.top(); queue.pop();
}
res.push_back({i, sum / 5});
}
return res;
}
};

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】1086. High Five 解题报告(C++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

  10. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

随机推荐

  1. 【GS文献】全基因组选择模型研究进展及展望

    目录 1. GS概况 2. GS模型 1)直接法 GBLUP 直接法的模型改进 ①单随机效应 ②多随机效应 2)间接法 间接法模型 基于间接法的模型改进 3. GS模型比较 模型比较结论 4.问题及展 ...

  2. 【宏组学】如何根据taxid(或taxname)快速获得taxname(或taxid)?

    需求 我有一个物种taxonomy ID的list,想获得相应的物种名,不要一个个去NCBI Taxonomy官网查.反之根据物种名list查询对应的taxid. 实现 因为之前没怎么用过,我的第一个 ...

  3. pheatmap() 的热图制作

    1.数据准备 2.画图 3.参数调整 (转自百迈克公众号) 关注下方公众号可获得更多精彩

  4. Selenium-IDE,在网页上模拟人的操作

    想偷懒,不想做很机械重复的网页操作,就百度了一下看看有什么方法,能把自己从重复性的网页操作中解放出来,于是,百度到了selenium ide,折腾许久,用最新版火狐添加了自带selenium ide组 ...

  5. Python压缩&解压缩

    Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...

  6. 巩固javaweb第十二天

    巩固内容: HTML 图像- 图像标签( <img>)和源属性(Src) 在 HTML 中,图像由<img> 标签定义. <img> 是空标签,意思是说,它只包含属 ...

  7. 移动开发之h5学习大纲

    移动开发学习形式:授课.自学 1.html5 css3 htm5shiv.js response.js 2.流式布局 自适应布局 盒模型 弹性盒模型 响应式布局3.iscroll swiper boo ...

  8. 实时数据同步inotify+rsync

    inotify inotify是一个实时监控服务,他能实时监控服务器中的目录的变化,发现目录中变化后,在配合rsync服务推送到备份服务器上 inotify要求内核要在2.6.13或之上,通过noti ...

  9. FastDFS分布式文件系统及源码解析

    记录一次本人学习FastDFS-分布式文件系统的学习过程,希望能帮助到有需要的人. 首选得对此技术有个大概的了解,可以参考 https://www.cnblogs.com/centos2017/p/7 ...

  10. ssm+ajax实现登陆

    ssm的搭建见上一章 1.数据协议层 public User selectByLoginnameAndPassword(@Param("loginname")String logi ...