• [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 (in other words erase it from the leaderboard). 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.

    力扣排行榜。

    新一轮的「力扣杯」编程大赛即将启动,为了动态显示参赛者的得分数据,需要设计一个排行榜 Leaderboard。

    请你帮忙来设计这个 Leaderboard 类,使得它有如下 3 个函数:

    addScore(playerId, score):
    假如参赛者已经在排行榜上,就给他的当前得分增加 score 点分值并更新排行。
    假如该参赛者不在排行榜上,就把他添加到榜单上,并且将分数设置为 score。
    top(K):返回前 K 名参赛者的 得分总和。
    reset(playerId):将指定参赛者的成绩清零(换句话说,将其从排行榜中删除)。题目保证在调用此函数前,该参赛者已有成绩,并且在榜单上。
    请注意,在初始状态下,排行榜是空的。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/design-a-leaderboard
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道设计题。我的思路如下,我们需要一个hashmap,一个treemap。除了题目要求实现的函数之外,我还需要一个helper函数。他的作用是当某一个player的分数有变动的时候,我需要将对应unique score记录的player的个数做出修改。其余部分参见代码。

    hashmap - <playerId, his/her score>

    treemap - <each unique score, how many people have this same score>

    时间O(n)

    空间O(n)

    Java实现

     1 class Leaderboard {
     2     // <playerId, score>
     3     HashMap<Integer, Integer> map;
     4     // <score, num of people>
     5     TreeMap<Integer, Integer> treemap;
     6 
     7     public Leaderboard() {
     8         map = new HashMap<>();
     9         treemap = new TreeMap<>();
    10     }
    11 
    12     public void addScore(int playerId, int score) {
    13         Integer curScore = map.get(playerId);
    14         if (curScore != null) {
    15             helper(curScore);
    16             curScore += score;
    17             map.put(playerId, curScore);
    18             if (treemap.containsKey(curScore)) {
    19                 treemap.put(curScore, treemap.get(curScore) + 1);
    20             } else {
    21                 treemap.put(curScore, 1);
    22             }
    23         } else {
    24             map.put(playerId, score);
    25             treemap.put(score, treemap.getOrDefault(score, 0) + 1);
    26         }
    27     }
    28 
    29     public int top(int K) {
    30         int sum = 0;
    31         Map.Entry<Integer, Integer> scoreEntry = treemap.lastEntry();
    32         while (K > 0 && scoreEntry != null) {
    33             int count = Math.min(scoreEntry.getValue(), K);
    34             sum += scoreEntry.getKey() * count;
    35             scoreEntry = treemap.lowerEntry(scoreEntry.getKey());
    36             K -= count;
    37         }
    38         return sum;
    39     }
    40 
    41     public void reset(int playerId) {
    42         int score = map.get(playerId);
    43         map.put(playerId, 0);
    44         helper(score);
    45         treemap.put(0, treemap.getOrDefault(0, 0) + 1);
    46     }
    47 
    48     private void helper(int score) {
    49         int count = treemap.get(score);
    50         if (count <= 1) {
    51             treemap.remove(score);
    52         } else {
    53             treemap.put(score, count - 1);
    54         }
    55     }
    56 }
    57 
    58 /**
    59  * Your Leaderboard object will be instantiated and called as such:
    60  * Leaderboard obj = new Leaderboard();
    61  * obj.addScore(playerId,score);
    62  * int param_2 = obj.top(K);
    63  * obj.reset(playerId);
    64  */

    LeetCode 题目总结

  • 相关阅读:
    NativeXml (1):下载、安装、测试
    NativeXml (7):添加属性
    NativeXml (9):读取
    NativeXml (2):对象建立
    NativeXml (3):保存
    NativeXml (6):添加节点
    NativeXml (10):编辑
    NativeXml (5):事件
    博客园现代化建设—用 Entity Framework 与 Json.NET 实现数据的按需更新
    Entity Framework 小知识分享
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14707301.html
Copyright © 2020-2023  润新知