题目如下:

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. TensorFlow实战第四课(tensorboard数据可视化)

    tensorboard可视化工具 tensorboard是tensorflow的可视化工具,通过这个工具我们可以很清楚的看到整个神经网络的结构及框架. 通过之前展示的代码,我们进行修改从而展示其神经网 ...

  2. 【Python开发】PyQt5应用与实践

    一个典型的GUI应用程序可以抽象为:主界面(菜单栏.工具栏.状态栏.内容区域),二级界面(模态.非模态),信息提示(Tooltip),程序图标等组成.本篇根据作者使用PyQt5编写的一个工具,介绍如何 ...

  3. SolidWorks学习笔记7 镜像,阵列

    镜像 将特征,面,实体相对于一个平面来复制.修改原来的特征,镜像特征随之改变 阵列 线性阵列 , 在左侧,先激活要阵列的特征,然后点击小柱 然后选择方向1和方向2,该方向的阵列距离和数量(一般使用边线 ...

  4. 将对象以json格式写入到文件中

    将 list 对象以json格式写入到文件中 try { ObjectMapper mapper = new ObjectMapper(); String value = mapper.writeVa ...

  5. String的非空判断:str!=""的为空判断出错问题

    if(str!=null && str!= ""){}这是错误的判断 String str1 = ""; String str2 = new S ...

  6. js同步任务和异步任务的执行顺序

    先来道今日头条面试题开开胃 async function async1() { console.log('async1 start'); await async2(); console.log('as ...

  7. FPGA —— Quartus II 15.0 使用 ModelSim SE-64 2019.2 软件进行仿真

    Quartus II 15.0 使用 ModelSim SE-64 2019.2 软件进行仿真 ModelSim 仿真 Verilog HDL 时需要编写一个 TestBench 仿真文件,通过仿真文 ...

  8. CDH安装前系统优化准备

    参考: https://www.cnblogs.com/yinzhengjie/p/10367447.html https://www.sysit.cn/blog/post/sysit/CDH6.2. ...

  9. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  10. Linx

    1. 2. 2. 3. 5. Vi 猜数字 第二十个裴伯拉数字 1 1 2 3 5 8 2 3 求小于3000的裴伯拉数列 4 5 递归方式1到100 和 6 7 100 以内奇数.偶数和 8 Sss ...