An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this:
The player with the highest score is ranked number \$1\$ on the leaderboard. Players who have equal scores receive the same ranking number, and the next player(s) receive the immediately following ranking number.
\$ranked = [100, 90, 90, 80]\$
\$player = [70, 80, 105]\$
The ranked players will have ranks 1, 2, 2, and 3, respectively. If the player's scores are 70, 80 and 105, their rankings after each game are 4th, 3rd and 1st. Return
[4, 3, 1].
Complete the climbingLeaderboard function in the editor below.
climbingLeaderboard has the following parameter(s):
int ranked[n]: the leaderboard scores
int player[m]: the player's scores
int[m]: the player's rank after each new score
The first line contains an integer \$n\$, the number of players on the leaderboard. The next line contains space-separated integers \$ranked[i]\$, the leaderboard scores in decreasing order. The next line contains an integer, \$m\$, the number games the player plays. The last line contains space-separated integers \$player[i]\$, the game scores.
- \$1 <= n <= 2 x 10^5\$
- \$1 <= m <= 2 x 10^5\$
- \$0 <= ranked[i] <= 10^9\$ for \$0 <= i < n\$
- The existing leaderboard, \$ranked\$, is in descending order.
- The player's scores, \$scores\$, are in ascending order.
For \$60%\$ of the maximum score:
- \$1 <= n <= 200\$
- \$1 <= m <= 200\$
Sample Input 1
7 100 100 50 40 40 20 10 4 5 25 50 120
Sample Output 1
6 4 2 1
I keep getting timed out. I think my general logic is right but how can my code be optimized more?
I know there are solutions using the bisect module but I've seen very simple ones without using that. From what I can think of, maybe using while loops will speed things up?
def climbingLeaderboard(ranked, player): ranked=[i for n, i in enumerate(ranked) if i not in ranked[:n]] #remove duplicates player_rank= ranking=len(ranked)+1 start=1 for pi,ps in enumerate(player[::-1]): for ri,r in enumerate(ranked,start=start): if ps>=r: player_rank.append(ri) break else: ranked=ranked[1:] start+=1 else: player_rank.append(ranking) return player_rank[::-1]