forked from rene-d/hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhackerland-radio-transmitters.py
34 lines (26 loc) · 1 KB
/
hackerland-radio-transmitters.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
# Algorithms > Search > Hackerland Radio Transmitters
# Find the minimum number of radio transmitters needed to cover all the houses in Hackerland!
#
# https://www.hackerrank.com/challenges/hackerland-radio-transmitters/problem
#
def hackerlandRadioTransmitters(n, x, k):
x = sorted(x)
nb = 0
i = 0
while i < n:
# essaie de placer l'émetteur le plus loin possible vers la droite
portee = x[i] + k # on cherche vers la droite le point qui couvre i
while i < n and x[i] <= portee: i += 1
i -= 1
# on place l'émetteur sur le point i
nb += 1
# saute les points couverts à droite
portee = x[i] + k # on saute tous les points couverts par l'émetteur en ri
while i < n and x[i] <= portee: i += 1
return nb
if __name__ == "__main__":
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
x = list(map(int, input().strip().split(' ')))
result = hackerlandRadioTransmitters(n, x, k)
print(result)