zoukankan      html  css  js  c++  java
  • 【leetcode】1244. Design A Leaderboard

    题目如下:

    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]
  • 相关阅读:
    Android手势(上,下,左和右的判断)
    我爱意甲
    程序员特有的9个坏习惯
    我爱英超
    VS2010快捷键总结(一)
    C#中导出Excel总结
    MessageDAL
    GDI+ 绘图总结
    .net中绑定日期时,只显示年月日的做法
    Vb线程控制
  • 原文地址:https://www.cnblogs.com/seyjs/p/11785284.html
Copyright © 2011-2022 走看看