-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc-13.31.py
More file actions
63 lines (55 loc) · 2.04 KB
/
Copy pathc-13.31.py
File metadata and controls
63 lines (55 loc) · 2.04 KB
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
"""
In the art gallery guarding problem we are given a line L that represents
a long hallway in an art gallery. We are also given a set X = {x0,x1,...,xn-1}
of real numbers that specify the positions of paintings in this hallway.
Suppose that a single guard can protect all the paintings
within distance at most 1 of his or her position (on both sides). Design
an algorithm for finding a placement of guards that uses the minimum
number of guards to guard all the paintings with positions in X.
"""
def position_guards(X):
"""
results with array of positions, where each position represents a guard and his position.
"""
output = []
paintings = []
potential_guard = None
old_len = 0
X.sort() # O(nlogn)
for idx, pos in enumerate(X):
potential_guard = potential_guard or pos
min_reach, max_reach = pos - 1.0, pos + 1.0
paintings = [pos]
k = idx - 1 # check +/- 1 radius of the position
while k >= 0 and X[k] >= min_reach:
paintings.append(X[k])
k -= 1
k = idx + 1
while k < len(X) and X[k] <= max_reach:
paintings.append(X[k])
k += 1
new_len = len(paintings)
if new_len > old_len:
potential_guard = pos
old_len = new_len
if k == idx + 1 and potential_guard is not None:
output.append(potential_guard)
potential_guard = None
old_len = 0
return output
if __name__ == "__main__":
X = [0.5, 2.5, 2.9, 3.54, 6, 6.3, 6.95, 9, 10, 10.5, 10.9, 15, 20]
guards = position_guards(X)
num_of_guards = len(guards)
assert guards == [0.5, 2.9, 6, 10, 15, 20]
assert num_of_guards == 6
X = [4.64, 7.96, 9.55, 0.01, 1.43, 4.49, 5.22, 6.31, 0.62, 7.22]
guards = position_guards(X)
num_of_guards = len(guards)
assert guards == [0.62, 4.49, 7.22, 9.55]
assert num_of_guards == 4
X = [0]
guards = position_guards(X)
num_of_guards = len(guards)
assert guards == [0]
assert num_of_guards == 1