https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
Solution
ref: https://inspirit941.tistory.com/199 Trying to solve it with Python list sort hit the time limit. Binary search didn’t help either.
def climbingLeaderboard(ranked, player): # Write your code here result = [] from collections import defaultdict rank_dict = defaultdict(int) for r in ranked: rank_dict[r] += 1
for p in player: score = list(rank_dict.keys()) score.append(p) score.sort(reverse=True) for idx, s in enumerate(score): if s == p: break result.append(idx + 1)
return resultTo fix the time limit, you can’t search from scratch for every p. The key insight is that player is in ascending order and ranked is in descending order, so you can skip regions that don’t need searching.
def climbingLeaderboard(ranked, player): queue = sorted(set(ranked), reverse=True)
idx = len(queue) - 1 result = []
for p in player: while queue[idx] <= p and idx >= 0: idx -= 1 if idx < 0: result.append(1) continue result.append(idx + 2) return resultBecause of the sort orders of player and ranked, earlier values in player correspond to later positions in ranked. So when iterating over player, we check ranked from the end. Once an index in the queue has been passed, there’s no need to revisit it. This way, no matter how large ranked is, we only traverse it once.
Negative Index
idx can become negative. This happens when all elements of ranked have been traversed. In that case, p is in 1st place, so we append 1 to the result.