leetcode 11/02/19, 11/03/19 周赛记录。

第一次参加周赛就单双周一起报了。刷的题还不多,因此每场只记录2-3道自己掌握的(比赛时提交通过或完了通不过,事后看一眼就明白怎么回事的)。Cuz my current goal is to get completely control of questions which are easy or medium-hard to solve(The label "hard" marked by LeetCode could be medium-hard or "medium" could be hard for me, it depends"). 保持我的习惯,还是用英文书写annotations. 

题目来自于leetcode

https://leetcode-cn.com/contest/weekly-contest-161

 

🏆第 12 场双周赛

第一题

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.

 

code:

class Leaderboard {
public:
    Leaderboard() {
        
    }
    
    void addScore(int playerId, int score) {
        map<int,int>::iterator find_player = player_score.find(playerId);
        if(find_player==player_score.end())
        {
            player_score.insert(map<int,int>::value_type(playerId, score));
        }else{
            player_score[playerId] += score;
        }
    }
    
    int top(int K) {
        vector<int> scores;
        for(auto it = player_score.begin(); it!=player_score.end();it++)
        {
            scores.push_back(it->second);
        }
        sort(scores.begin(), scores.end());
        int res = 0;
        for(int i = 0; i<K; i++)
        {
            res += scores[scores.size()-i-1];
        }
        return res;
    }
    
    void reset(int playerId) {
        player_score[playerId] = 0;
    }
private:
    map<int,int> player_score;
};

 

comment:

This is really not a medium level of question, it's "so easy". only the function "int top(int K)" need to do some "algorithm". And we can use #include <algorithm>'s "sort()" function to deal with it. However, sort() can only sort a "vector" type, we gotta push the whole map into a vector before sorting.

 

第二题

1243. Array Transform

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值