-
Notifications
You must be signed in to change notification settings - Fork 17
/
climbing-the-leaderboard.py
72 lines (49 loc) · 1.52 KB
/
climbing-the-leaderboard.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
66
67
68
69
70
71
72
#!/bin/python3
import math
import os
import random
import re
import sys
from typing import List
#
# Complete the 'climbingLeaderboard' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY ranked
# 2. INTEGER_ARRAY player
#
def climbingLeaderboard(ranked: List[int], player: List[int]) -> List[int]:
ranks = [1] * len(ranked)
rank = 1
for pos in range(1, len(ranked)):
if ranked[pos] != ranked[pos - 1]:
rank += 1
ranks[pos] = rank
def bisect(score: int) -> int:
left, right = 0, len(ranked) - 1
while left < right:
middle = left + (right - left) // 2
if ranked[middle] <= score:
right = middle
else:
left = middle + 1
return left
result: List[int] = [0] * len(player)
for pos in range(len(player)):
ranked_pos = bisect(player[pos])
if ranked[ranked_pos] <= player[pos]:
result[pos] = ranks[ranked_pos]
else:
result[pos] = ranks[ranked_pos] + 1
return result
if __name__ == "__main__":
fptr = open(os.environ["OUTPUT_PATH"], "w")
ranked_count = int(input().strip())
ranked = list(map(int, input().rstrip().split()))
player_count = int(input().strip())
player = list(map(int, input().rstrip().split()))
result = climbingLeaderboard(ranked, player)
fptr.write("\n".join(map(str, result)))
fptr.write("\n")
fptr.close()