-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHRPSA0017.py
65 lines (51 loc) · 2 KB
/
HRPSA0017.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Solution for "Climbing the leaderboard"
#Link: https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem?isFullScreen=true
#Problem Solving-> Algorithms-> Data Structures
def climbingLeaderboard(ranked, player):
ranked = sorted(set(ranked), reverse=True)
player_ranks = []
i = len(ranked) -1
for score in player:
while i >= 0 and score >= ranked[i]:
i -= 1
if i == -1:
player_ranks.append(1)
else:
player_ranks.append(i + 2)
return player_ranks
NnumPlayer=int(input())
scores=input()
scoresArr=list(map(int,scores.split()))
MnumGames=int(input())
playerScores=input()
playerScoresArr=list(map(int,playerScores.split()))
result=climbingLeaderboard(scoresArr,playerScoresArr)
for i in range(0,len(result)):
print(result[i])
#Alternative way (using Pandas library)
import pandas as pd
def climbingLeaderboard(ranked, player):
# Create a DataFrame for ranked scores
df_ranked = pd.DataFrame(ranked, columns=['Score'])
# Remove duplicate scores and reindex
df_ranked = df_ranked['Score'].drop_duplicates().reset_index(drop=True)
# Create a DataFrame for player scores
df_player = pd.DataFrame(player, columns=['Score'])
# Add a new column for dense rank to the ranked DataFrame
df_ranked['Rank'] = df_ranked['Score'].rank(method='dense', ascending=False)
# Iterate through player scores
for i in range(len(df_player)):
score = df_player.loc[i, 'Score']
# Find the rank for the player's score using the ranked DataFrame
rank = df_ranked[df_ranked['Score'] >= score]['Rank'].min()
# If the player's score is higher than all on the leaderboard
if pd.isna(rank):
rank = len(df_ranked) + 1
print(int(rank))
NnumPlayer = int(input())
scores = input()
scoresArr = list(map(int, scores.split()))
MnumGames = int(input())
playerScores = input()
playerScoresArr = list(map(int, playerScores.split()))
climbingLeaderboard(scoresArr, playerScoresArr)