题目如下:

Design a Leaderboard class, which has 3 functions:

  1. addScore(playerId, score): Update the leaderboard by adding score to the given player's score. If there is no player with such id in the leaderboard, add him to the leaderboard with the given score.
  2. top(K): Return the score sum of the top K players.
  3. reset(playerId): Reset the score of the player with the given id to 0. It is guaranteed that the player was added to the leaderboard before calling this function.

Initially, the leaderboard is empty.

Example 1:

Input:
["Leaderboard","addScore","addScore","addScore","addScore","addScore","top","reset","reset","addScore","top"]
[[],[1,73],[2,56],[3,39],[4,51],[5,4],[1],[1],[2],[2,51],[3]]
Output:
[null,null,null,null,null,null,73,null,null,null,141] Explanation:
Leaderboard leaderboard = new Leaderboard ();
leaderboard.addScore(1,73); // leaderboard = [[1,73]];
leaderboard.addScore(2,56); // leaderboard = [[1,73],[2,56]];
leaderboard.addScore(3,39); // leaderboard = [[1,73],[2,56],[3,39]];
leaderboard.addScore(4,51); // leaderboard = [[1,73],[2,56],[3,39],[4,51]];
leaderboard.addScore(5,4); // leaderboard = [[1,73],[2,56],[3,39],[4,51],[5,4]];
leaderboard.top(1); // returns 73;
leaderboard.reset(1); // leaderboard = [[2,56],[3,39],[4,51],[5,4]];
leaderboard.reset(2); // leaderboard = [[3,39],[4,51],[5,4]];
leaderboard.addScore(2,51); // leaderboard = [[2,51],[3,39],[4,51],[5,4]];
leaderboard.top(3); // returns 141 = 51 + 51 + 39;

Constraints:

  • 1 <= playerId, K <= 10000
  • It's guaranteed that K is less than or equal to the current number of players.
  • 1 <= score <= 100
  • There will be at most 1000 function calls.

解题思路:根据本题对性能要求不是很高,我的方法是记录每个分数出现的次数,top()的时候把分数排序,再加上每个分数的次数,即可求出排名。

代码如下:

class Leaderboard(object):
def __init__(self):
self.dic = {}
self.dic_player = {}
def addScore(self, playerId, score):
"""
:type playerId: int
:type score: int
:rtype: None
"""
if playerId not in self.dic_player:
self.dic_player[playerId] = score
self.dic[score] = self.dic.setdefault(score,0) + 1
else:
ori_score = self.dic_player[playerId]
self.dic[ori_score] -= 1
self.dic_player[playerId] += score
score = self.dic_player[playerId]
self.dic[score] = self.dic.setdefault(score, 0) + 1 def top(self, K):
"""
:type K: int
:rtype: int
"""
score_list = sorted(self.dic.iterkeys())[::-1]
res = 0
for i in range(len(score_list)):
if K == 0:break
elif K >= self.dic[score_list[i]]:
res += self.dic[score_list[i]] * score_list[i]
K -= self.dic[score_list[i]]
elif K < self.dic[score_list[i]]:
res += K * score_list[i]
K = 0
return res def reset(self, playerId):
"""
:type playerId: int
:rtype: None
"""
score = self.dic_player[playerId]
self.dic[score] -= 1
if self.dic[score] == 0:
del self.dic[score]
del self.dic_player[playerId]

【leetcode】1244. Design A Leaderboard的更多相关文章

  1. 【LeetCode】1166. Design File System 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...

  2. 【LeetCode】355. Design Twitter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  4. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  5. 【LeetCode】707. Design Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】705. Design HashSet 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...

  8. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  9. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

随机推荐

  1. 非常好的一个JS代码(FixedMenu.htm)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. json字符串转成 json对象 json对象转换成java对象

    import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> ...

  3. 多线程--原子操作 Interlocked系列函数

    [转]原文地址:http://blog.csdn.net/morewindows/article/details/7429155 线程同步与互斥: 互斥主要指多个线程不能同时访问一个资源,如打印机就是 ...

  4. oracle查询表的结构

    SELECT t.table_name,t.column_name,t.data_type||'('||t.data_length||')', t1.comments FROM User_Tab_Co ...

  5. 【B2B】01-BFS

    纠正我对 01-BFS 问题的错误认识. 我一直以为对于 01-BFS,每次点 $u$ 出队时,对于 $u$ 的邻接边表中的边,只要先松弛边权为 0 的边再松弛边权为 1 的边就能保证每个点只入队一次 ...

  6. 使用Keras基于AdvancedEAST的场景图像文本检测

    Blog:https://blog.csdn.net/linchuhai/article/details/84677249 GitHub:https://github.com/huoyijie/Adv ...

  7. 调用webService学习小结

    这段时间项目进行到了最后时刻,但是还有很多需求没有搞清楚,眼看deadline越来越近,压力也越来越大.现在我的主要工作是将别人开发好的一个系统给加载到我们系统中,使用的方法是通过webService ...

  8. MySQL的变量

    MySQL的变量 变量分两种,系统变量和用户变量 来源:https://blog.csdn.net/J080624/article/details/73828012 [1]系统变量 系统定义好的变量, ...

  9. 07 MySQL之索引原理

    一.介绍 为什么有索引:使用索引可快速访问数据库表中的特定信息.索引是对数据库表中一列或多列的值进行排序的一种结构. 作用: 1.        快速查询数据 2.        保证数据的唯一性 3 ...

  10. mybatis的if

    <select id="findList" resultType="BndExport"> SELECT <include refid=&qu ...