hard

1.
SpeedCameras

Position speed cameras so as to minimize the lengths of unmonitored paths.
100%

100%

100%

Recently, more and more illegal street races have been spotted at night in the city, and they have become a serious threat to public safety. Therefore, the Police Chief has decided to deploy speed cameras on the streets to collect evidence.

There are N+1 intersections in the city, connected by N roads. Every road has the same length of 1. A street race may take place between any two different intersections by using the roads connecting them. Limited by their budget, the police are able to deploy at most K speed cameras on these N roads. These K speed cameras should be installed such that the length of any possible street race route not covered by speed cameras should be as short as possible.

You are given a map of the city in the form of two arrays, A and B of length N, and an integer K:

- For each J (0 ≤ J < N) there is a road connecting intersections A[J] and B[J].

The Police Chief would like to know the minimum length of the longest path out of surveillance after placing at most K speed cameras.

Write a function:

def solution(A, B, K)

that, given arrays A and B of N integers and integer K, returns the minimum length of the longest path unmonitored by speed cameras after placing at most K speed cameras.

For example, given K = 2 and the following arrays:

the function should return 2. Two speed cameras can be installed on the roads between intersections 1 and 0 and between intersections 0 and 7. (Another solution would be to install speed cameras between intersections 0 and 7 and between intersections 0 and 6.) By installing speed cameras according the first plan, one of the longest paths without a speed camera starts at intersection 8, passes through intersection 6 and ends at intersection 3, which consists of two roads. (Other longest paths are composed of intersections 5, 1, 9 and 7, 2, 4).

Write an ** efficient** algorithm for the following assumptions:

- N is an integer within the range [1..50,000];
- each element of arrays A, B is an integer within the range [0..N];
- K is an integer within the range [0..N];
- the distance between any two intersections is not greater than 900.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

Solution

Programming language used Python

Total time used 2 minutes

Effective time used 3 minutes

Notes
*not defined yet*

Task timeline

Code: 15:45:37 UTC,
py,
verify,
result: **Passed**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

Analysis

Code: 15:46:15 UTC,
py,
verify,
result: **Passed**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

User test case 1:

([0], [1])

Analysis

expand all
**User tests**

1.

0.001 s

Code: 15:46:34 UTC,
py,
verify,
result: **Passed**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

User test case 1:

[[0], [1], 0]

Analysis

Code: 15:46:59 UTC,
py,
verify,
result: **Passed**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

User test case 1:

[[0], [1], 1]

Analysis

Code: 15:47:09 UTC,
py,
verify,
result: **Passed**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

User test case 1:

[[0], [1], 1]

Analysis

Code: 15:47:14 UTC,
py,
final,
score: **
100
**

```
def solution(A, B, K):
M = len(A) + 1
class Node(object):
def __init__(self, idx):
self.idx = idx
self.neighbours = []
self.children = []
self.height = -1
self.parent = None
graph = [Node(idx) for idx in xrange(M)]
for v1, v2 in zip(A, B):
graph[v1].neighbours.append(graph[v2])
graph[v2].neighbours.append(graph[v1])
def root(node, parent=None):
node.parent = parent
for child in node.neighbours:
if child is parent: continue
node.children.append(child)
root(child, node)
root(graph[0])
def needed_cameras(node, allowed_diameter):
result = 0
node.height = 0
for child in node.children:
result += needed_cameras(child, allowed_diameter)
sigh = sorted((child.height, cid, child)
for cid, child in enumerate(node.children)
if child.height < allowed_diameter)
sigh = [child for _, _, child in sigh]
result += len(node.children) - len(sigh)
while len(sigh) > 1 and sigh[-1].height + sigh[-2].height + 2 > allowed_diameter:
sigh.pop()
result += 1
for child in sigh:
node.height = max(node.height, 1 + child.height)
return result
left = 0
right = 900
while left < right:
mid = (left + right) / 2
need = needed_cameras(graph[0], mid)
if need <= K:
right = mid
else:
left = mid + 1
return left
```

Analysis summary

The solution obtained perfect score.

Analysis

Detected time complexity:

expand all
**Correctness tests**

1.

0.067 s

2.

0.067 s

3.

0.067 s

4.

0.067 s

1.

0.068 s

2.

0.068 s

3.

0.068 s

4.

0.067 s

1.

0.068 s

2.

0.068 s

3.

0.068 s

4.

0.068 s

5.

0.068 s

1.

0.067 s

2.

0.065 s

3.

0.068 s

4.

0.070 s

5.

0.066 s

6.

0.067 s

7.

0.070 s

8.

0.068 s

1.

0.070 s

2.

0.070 s

3.

0.072 s

1.

0.075 s

2.

0.078 s

3.

0.077 s

4.

0.077 s

1.

0.249 s

2.

0.238 s

3.

0.239 s

4.

0.184 s

expand all
**Correctness/performance tests**

1.

1.812 s

2.

1.874 s

1.

1.783 s

2.

1.919 s

1.

2.098 s

2.

2.081 s

3.

2.047 s

1.

1.913 s

2.

1.894 s

1.

2.035 s

2.

2.022 s