-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy_antennas.py
75 lines (58 loc) · 2.36 KB
/
greedy_antennas.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
73
74
75
"""
Consider a rectilinear road of t kilometers.
Among the road there are k houses,
the i_th of which at km d_i on the road.
🏠 🏠 🏠 🏠
|---|-------|-----------|------|----|
d1 d2 ... d3 dk t km
A GSM company wants to build antennas so to
cover each house with its network.
Given that an antenna covers a radius
of 10 KMs, what is the smallest number of antennas
that have to be built?
"""
from heapsort import heapsorted as sorted
def antennas(houses, radius):
"""
args:
houses: list
such that houses[i] == d means that
the i_th house is located at the d_th kilometer
radius: int, radius covered by an antenna
returns:
locations: list
such that locations[i] == d means that
the i_th antenna should be placed at the d_th kilometer
"""
locations = []
houses = sorted(houses, reverse=True)
while houses:
# place antenna at first uncovered house location + radius
antenna = houses.pop() + radius
locations.append(antenna)
# remove all houses now covered by the new antenna
i = antenna - radius
j = antenna + radius
house = len(houses) - 1
while house >= 0 and i <= houses[house] <= j:
house -= 1
houses.pop()
return locations
if __name__ == "__main__":
# 🏠 🏠 🏠
# [ 📡 ] [ 📡 ]
# |----|-----|-----|------------|-------|------|-------|
# 0 3 9 13 23 30 40 50
houses = [9, 3, 30]
locations = antennas(houses, radius=10)
assert locations == [13, 40]
print(f"{len(locations)} antennas, placed at kilometers {locations}")
##################################################
# 🏠 🏠 🏠 🏠 🏠 🏠
# [ 📡 ] [ 📡 ] [ 📡 ]
# |----|-----|-----|----|----|----|----|-----|------|------|-----|-----|
# 0 3 8 13 15 19 23 26 36 46 100 110 120
houses = [3, 26, 19, 8, 15, 100]
locations = antennas(houses, radius=10)
assert locations == [13, 36, 110]
print(f"{len(locations)} antennas, placed at kilometers {locations}")