【leetcode】1244. Design A Leaderboard
题目如下:
Design a Leaderboard class, which has 3 functions:
addScore(playerId, score)
: Update the leaderboard by addingscore
to the given player's score. If there is no player with such id in the leaderboard, add him to the leaderboard with the givenscore
.top(K)
: Return the score sum of the topK
players.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的更多相关文章
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】705. Design HashSet 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- 【leetcode】622. Design Circular Queue
题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...
随机推荐
- 前端 api 请求缓存方案
参考链接:https://blog.csdn.net/zhuoganliwanjin/article/details/89598753#commentBox
- 瀑布布局(waterflall flow)实现
瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动.这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pinterest,逐渐 ...
- MSF魔鬼训练营-5.3 MS08-067安全漏洞实战
msf > search ms08_067 Matching Modules ================ Name D ...
- 自己总结的keepalived的配置流程以及注意事项
编写背景:上班时领导要求我们团队实现postgresql主备切换的高可用问题,我辅助keepalived的部分,从查资料到实施最后使用,最后编写了这个博客,水平有限,欢迎大家指正 ###postgre ...
- Python学习【day02】- 运算符与基本类型
Python语言支持以下类型的运算符: 算术运算符 操作符 描述 示例(a=10.b=21) + 加法 相加运算两侧的值 a + b = 31 - 减法 操作符右侧数减去左侧操作数 a – b = - ...
- Python学习【day01】- Python初识
Python下载:https://www.python.org/downloads/ 下载后进行安装,安装后的Python我们称之为Python解析器 1.打印“Hello World” 安装后的Py ...
- JDK安装及JAVA环境变量配置(JDK1.8版本)
一:JDK官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html JD ...
- 四、redis学习(案例)
效果如下 注意:必须要开启redis服务器端 数据库 CREATE DATABASE heima; -- 创建数据库 USE heima; -- 使用数据库 CREATE TABLE province ...
- LintCode 29---交叉字符串
public class Solution { /** * @param s1: A string * @param s2: A string * @param s3: A string * @ret ...
- 淘宝flexible.js的使用
首先大家最关注的怎么使用,原理不原理是后面的事 比如设计稿量来的宽度是100px 那么我们布局的时候,就这么写{width:1.3333rem},1.3333rem是由100/75算出来的,以此类推2 ...