领扣LintCode算法问题答案-1483. 最高平均分
目录
- 1483. 最高平均分
-
- 描述
- 样例 1:
- 样例 2:
- 题解
- 鸣谢
1483. 最高平均分
描述
给出一组学生的名字以及他们的成绩,求最高的平均分(一位学生可能有多门课成绩)
样例 1:
输入:names = ["bob","ted","ted"]
grades=[88,100,20]
输出:88
样例 2:
输入: names = ["john","xisa","xisa","liajd","alice","john","xisa","mark","ted","xlisa"]
grades = [95,83,33,50,78,91,80,67,85,87]
输出:93
题解
public class Solution {
/*** @param names: the name* @param grades: the grade* @return: the maximum average score*/public double maximumAverageScore(List<String> names, int[] grades) {
// Write your code hereMap<String, Double> totalScoreMap = new HashMap<>();Map<String, Long> counter = new HashMap<>();int i = 0;for (String name : names) {
int grade = grades[i++];Double totalScore = totalScoreMap.get(name);if (totalScore == null) {
totalScore = 0.0;}totalScore += grade;totalScoreMap.put(name, totalScore);Long count = counter.get(name);if (count == null) {
count = 0l;}count++;counter.put(name, count);}double maximumAverageScore = 0;for (Map.Entry<String, Double> row : totalScoreMap.entrySet()) {
String name = row.getKey();double totalScore = row.getValue();Long count = counter.get(name);double avgScore = totalScore / count;if (avgScore > maximumAverageScore) {
maximumAverageScore = avgScore;}}return maximumAverageScore;}
}
原题链接点这里
鸣谢
非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。
欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。