Skip to content

Commit 6db6506

Browse files
committed
feat: add solutions to leetcode problem: No.1244. Design A Leaderboard
1 parent fca4e53 commit 6db6506

File tree

4 files changed

+164
-5
lines changed

4 files changed

+164
-5
lines changed

solution/1200-1299/1244.Design A Leaderboard/README.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
<ol>
1414
<li><code>addScore(playerId, score)</code>:
15-
1615
<ul>
1716
<li>假如参赛者已经在排行榜上,就给他的当前得分增加 <code>score</code> 点分值并更新排行。</li>
1817
<li>假如该参赛者不在排行榜上,就把他添加到榜单上,并且将分数设置为 <code>score</code>。</li>
@@ -60,27 +59,82 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39;
6059
<li>最多进行 <code>1000</code> 次函数调用</li>
6160
</ul>
6261

63-
6462
## 解法
6563

6664
<!-- 这里可写通用的实现逻辑 -->
6765

66+
用哈希表存放每个 playerId 所对应的分数。
67+
68+
计算 topK 时,取出所有的分数,进行排序,获取前 K 个分数,累加得到结果。
69+
6870
<!-- tabs:start -->
6971

7072
### **Python3**
7173

7274
<!-- 这里可写当前语言的特殊实现逻辑 -->
7375

7476
```python
77+
class Leaderboard:
78+
79+
def __init__(self):
80+
self.player_scores = {}
7581

82+
def addScore(self, playerId: int, score: int) -> None:
83+
self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score
84+
85+
def top(self, K: int) -> int:
86+
scores = sorted(list(self.player_scores.values()), reverse=True)
87+
return sum(scores[:K])
88+
89+
def reset(self, playerId: int) -> None:
90+
self.player_scores[playerId] = 0
91+
92+
93+
# Your Leaderboard object will be instantiated and called as such:
94+
# obj = Leaderboard()
95+
# obj.addScore(playerId,score)
96+
# param_2 = obj.top(K)
97+
# obj.reset(playerId)
7698
```
7799

78100
### **Java**
79101

80102
<!-- 这里可写当前语言的特殊实现逻辑 -->
81103

82104
```java
83-
105+
class Leaderboard {
106+
private Map<Integer, Integer> playerScores;
107+
108+
public Leaderboard() {
109+
playerScores = new HashMap<>();
110+
}
111+
112+
public void addScore(int playerId, int score) {
113+
playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score);
114+
}
115+
116+
public int top(int K) {
117+
List<Integer> scores = new ArrayList<>(playerScores.values());
118+
Collections.sort(scores, Collections.reverseOrder());
119+
int res = 0;
120+
for (int i = 0; i < K; ++i) {
121+
res += scores.get(i);
122+
}
123+
return res;
124+
}
125+
126+
public void reset(int playerId) {
127+
playerScores.put(playerId, 0);
128+
}
129+
}
130+
131+
/**
132+
* Your Leaderboard object will be instantiated and called as such:
133+
* Leaderboard obj = new Leaderboard();
134+
* obj.addScore(playerId,score);
135+
* int param_2 = obj.top(K);
136+
* obj.reset(playerId);
137+
*/
84138
```
85139

86140
### **...**

solution/1200-1299/1244.Design A Leaderboard/README_EN.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,72 @@ leaderboard.top(3); // returns 141 = 51 + 51 + 39;
4848
<li>There will be at most <code>1000</code>&nbsp;function calls.</li>
4949
</ul>
5050

51-
5251
## Solutions
5352

5453
<!-- tabs:start -->
5554

5655
### **Python3**
5756

5857
```python
58+
class Leaderboard:
59+
60+
def __init__(self):
61+
self.player_scores = {}
62+
63+
def addScore(self, playerId: int, score: int) -> None:
64+
self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score
65+
66+
def top(self, K: int) -> int:
67+
scores = sorted(list(self.player_scores.values()), reverse=True)
68+
return sum(scores[:K])
5969

70+
def reset(self, playerId: int) -> None:
71+
self.player_scores[playerId] = 0
72+
73+
74+
# Your Leaderboard object will be instantiated and called as such:
75+
# obj = Leaderboard()
76+
# obj.addScore(playerId,score)
77+
# param_2 = obj.top(K)
78+
# obj.reset(playerId)
6079
```
6180

6281
### **Java**
6382

6483
```java
65-
84+
class Leaderboard {
85+
private Map<Integer, Integer> playerScores;
86+
87+
public Leaderboard() {
88+
playerScores = new HashMap<>();
89+
}
90+
91+
public void addScore(int playerId, int score) {
92+
playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score);
93+
}
94+
95+
public int top(int K) {
96+
List<Integer> scores = new ArrayList<>(playerScores.values());
97+
Collections.sort(scores, Collections.reverseOrder());
98+
int res = 0;
99+
for (int i = 0; i < K; ++i) {
100+
res += scores.get(i);
101+
}
102+
return res;
103+
}
104+
105+
public void reset(int playerId) {
106+
playerScores.put(playerId, 0);
107+
}
108+
}
109+
110+
/**
111+
* Your Leaderboard object will be instantiated and called as such:
112+
* Leaderboard obj = new Leaderboard();
113+
* obj.addScore(playerId,score);
114+
* int param_2 = obj.top(K);
115+
* obj.reset(playerId);
116+
*/
66117
```
67118

68119
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Leaderboard {
2+
private Map<Integer, Integer> playerScores;
3+
4+
public Leaderboard() {
5+
playerScores = new HashMap<>();
6+
}
7+
8+
public void addScore(int playerId, int score) {
9+
playerScores.put(playerId, playerScores.getOrDefault(playerId, 0) + score);
10+
}
11+
12+
public int top(int K) {
13+
List<Integer> scores = new ArrayList<>(playerScores.values());
14+
Collections.sort(scores, Collections.reverseOrder());
15+
int res = 0;
16+
for (int i = 0; i < K; ++i) {
17+
res += scores.get(i);
18+
}
19+
return res;
20+
}
21+
22+
public void reset(int playerId) {
23+
playerScores.put(playerId, 0);
24+
}
25+
}
26+
27+
/**
28+
* Your Leaderboard object will be instantiated and called as such:
29+
* Leaderboard obj = new Leaderboard();
30+
* obj.addScore(playerId,score);
31+
* int param_2 = obj.top(K);
32+
* obj.reset(playerId);
33+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Leaderboard:
2+
3+
def __init__(self):
4+
self.player_scores = {}
5+
6+
def addScore(self, playerId: int, score: int) -> None:
7+
self.player_scores[playerId] = self.player_scores.get(playerId, 0) + score
8+
9+
def top(self, K: int) -> int:
10+
scores = sorted(list(self.player_scores.values()), reverse=True)
11+
return sum(scores[:K])
12+
13+
def reset(self, playerId: int) -> None:
14+
self.player_scores[playerId] = 0
15+
16+
17+
# Your Leaderboard object will be instantiated and called as such:
18+
# obj = Leaderboard()
19+
# obj.addScore(playerId,score)
20+
# param_2 = obj.top(K)
21+
# obj.reset(playerId)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy