-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathclimbing-the-leaderboard.py
61 lines (46 loc) · 1.35 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
# Climbing the Leaderboard
# Help Alice track her progress toward the top of the leaderboard!
#
# https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
#
"""
solution 'à la mano'
on peut aussi utiliser un set() à la place de groupby(),
puis sorted() et bisect.bisect_right() pour faire la dichotomie
ce qui revient +in fine+ à la même chose
"""
import itertools
# dichotomie dans une liste décroissante
def binary_search(a, x):
first = 0
last = len(a) - 1
while first <= last:
i = (first + last) // 2
if a[i] == x:
return i
elif a[i] < x:
last = i - 1
else:
first = i + 1
return first
def climbingLeaderboard(scores, alice):
# Complete this function
leaderboard = [s for s, _ in itertools.groupby(scores)]
for a in alice:
""" version triviale
rank = 1
for s in leaderboard:
if a >= s:
break
rank += 1
"""
# version optimisée avec dichotomie
# nécessaire pour passer certains testcases
rank = binary_search(leaderboard, a) + 1
print(rank)
if __name__ == "__main__":
n = int(input())
scores = list(map(int, input().split()))
m = int(input().strip())
alice = list(map(int, input().split()))
climbingLeaderboard(scores, alice)