1209. Remove All Adjacent Duplicates in String II
in Coding Interview on Stack, String
3가지 기능이 있는 Leaderboard 클래스를 설계합니다.
- addScore(playerId, score): 주어진 플레이어의 점수에 점수를 추가하여 순위표를 업데이트
- top(k): 상위 K 플레이어의 점수 합계를 반환
- reset(playerId): 주어진 ID를 가진 플레이어의 점수를 0으로 재설정(즉, 리더보드에서 삭제)
class Leaderboard {
Map<Integer, Integer> scores;
TreeMap<Integer, Integer> sortedScores;
public Leaderboard() {
this.scores = new HashMap<Integer, Integer>();
this.sortedScores = new TreeMap<>(Collections.reverseOrder());
}
public void addScore(int playerId, int score) {
// The scores dictionary simply contains the mapping from the
// playerId to their score. The sortedScores contain a BST with
// key as the score and value as the number of players that have
// that score.
if (!this.scores.containsKey(playerId)) {
this.scores.put(playerId, score);
this.sortedScores.put(score, this.sortedScores.getOrDefault(score, 0) + 1);
} else {
// Since the current player's score is changing, we need to
// update the sortedScores map to reduce count for the old
// score.
int preScore = this.scores.get(playerId);
int playerCount = this.sortedScores.get(preScore);
// If no player has this score, remov it from the tree.
if (playerCount == 1) {
this.sortedScores.remove(preScore);
} else {
this.sortedScores.put(preScore, playerCount - 1);
}
// Updated score
int newScore = preScore + score;
this.scores.put(playerId, newScore);
this.sortedScores.put(newScore, this.sortedScores.getOrDefault(newScore, 0) + 1);
}
}
public int top(int K) {
int count = 0;
int sum = 0;
// In-order traversal over the scores in the TreeMap
for (Map.Entry<Integer, Integer> entry: this.sortedScores.entrySet()) {
// Number of players that have this score.
int times = entry.getValue();
int key = entry.getKey();
for (int i = 0; i < times; i++) {
sum += key;
count++;
// Found top-K scores, break.
if (count == K) {
break;
}
}
// Found top-K scores, break.
if (count == K) {
break;
}
}
return sum;
}
public void reset(int playerId) {
int preScore = this.scores.get(playerId);
this.sortedScores.put(preScore, this.sortedScores.get(preScore) - 1);
if (this.sortedScores.get(preScore) == 0) {
this.sortedScores.remove(preScore);
}
this.scores.remove(playerId);
}
}
/**
* Your Leaderboard object will be instantiated and called as such:
* Leaderboard obj = new Leaderboard();
* obj.addScore(playerId,score);
* int param_2 = obj.top(K);
* obj.reset(playerId);
*/