题目地址: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. 优势杂交育种预测 2. GS育种原理与模型算法 岭回归和LASSO回归 贝叶斯方法 GBLUP和RRBLUP 偏最小二乘法 支持向量机/支持向量回归 其他方法 3. 模型预测能力验证 4. ...

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

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

  3. VMware和Centos的安装及配置

    目录 1. 安装VMware 2. 安装CentOS6及配置 2.1 Centos安装 2.1.1 配置网络连接的三种形式 2.1.1.1 桥连接 2.1.1.2 NAT模式 2.1.1.3 主机模式 ...

  4. Demo01无重复数字

    package 习题集2;//有1,2,3,4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?public class Demo1 { public static void main(S ...

  5. IPFS是什么?IPFS原理、IPFS存储

    以下内容调研截止到2021/11/5日 IPFS简介 IPFS是一种内容可寻址.点对点.分布式文件系统.IPFS采用内容-地址寻址技术,即通过文件内容进行检索而不是通过文件的网络地址.简单来说,就是对 ...

  6. MapReduce06 MapReduce工作机制

    目录 5 MapReduce工作机制(重点) 5.1 MapTask工作机制 5.2 ReduceTask工作机制 5.3 ReduceTask并行度决定机制 手动设置ReduceTask数量 测试R ...

  7. A Child's History of England.1

    A Child's History of England, by Charles Dickens (狄更斯) CHAPTER I ANCIENT ENGLAND AND THE ROMANS If y ...

  8. acre, across

    acre The acre is a unit of land area used in the imperial and US customary systems. It is traditiona ...

  9. stm32串行设备接口SPI控制max31865

    本人是刚入行的嵌入式,之前也没有多少项目经验,故在公司的这几个月里,可谓是如履薄冰,对于公司不同项目使用的不同的设备之多,数据手册之繁杂,让我不禁望洋兴叹,故而不愿意放弃周末这大好的自我提升时间,努力 ...

  10. 一份不错的Java就业指导

    想要成为合格的Java程序员或工程师到底需要具备哪些专业技能,面试者在面试之前到底需要准备哪些东西呢? 本文陈列的这些内容既可以作为个人简历中的内容,也可以作为面试的时候跟面试官聊的东西,你可以把这些 ...