LeetCode题解(1086):前五科的均分(Python)
发布日期:2021-06-29 20:09:43 浏览次数:2 分类:技术文章

本文共 598 字,大约阅读时间需要 1 分钟。

题目:(简单)

标签:哈希表、堆、排序、数组

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N) 28ms (100.00%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def highFive(self, items: List[List[int]]) -> List[List[int]]:        # 统计学生得分        count = collections.defaultdict(list)        for student, mark in items:            heapq.heappush(count[student], mark)            if len(count[student]) > 5:                heapq.heappop(count[student])        # 计算学生平均分        ans = [[student, sum(lst) // 5] for student, lst in count.items()]        ans.sort(key=lambda x: x[0])        return ans

转载地址:https://dataartist.blog.csdn.net/article/details/109845132 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1090):受标签影响的最大值(Python)
下一篇:LeetCode题解(1072):按列翻转得到最大值等行数(Python)

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月20日 15时06分11秒