Skip to content

Commit 55f2ee7

Browse files
author
Joseph Luce
authored
Update Minimize_road_between_stations.md
1 parent 2a815bb commit 55f2ee7

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

real_interview_questions/Google/Minimize_road_between_stations.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
1+
# QUESTION
2+
There is long motorway of length M (an integer).
3+
On the motorway at some points (integers) there are petrol stations (there is a given integer array T[] of length N - the positions of petrol stations).
4+
We have funds to add at most K new petrol stations on this motorway.
5+
We want to minimize largest stretch of the motorway between petrol stations.
6+
You can put petrol stations on integer positions.
7+
8+
1. Design and implement a function that calculates the minimum achievable uncovered motorway length.
9+
2. Write testcases for your algorithm.
10+
3. (Bonus) Analyze your algorithm's runtime and space complexity.
11+
4. (Bonus) Suggest optimizations of your algorithm.
12+
5. (Bonus) Modify the function from part 1 to output positions at which the new stations can be added to
13+
achieve the optimum uncovered motorwary length.
14+
15+
Example:
16+
N = 5, M = 20, K = 3, T = [3, 7, 15, 18, 1]
17+
Minimum uncovered : 3 (obtainable for example by adding petrol stations at positions: 5, 10 and 13)
18+
19+
St-Ps----Ps----Np----Ps-------Np-------Np----Ps-------Ps----Fn
20+
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
21+
22+
St - start of motorway
23+
Fn - finish of motorway
24+
Ps - pre-existing petrol station
25+
Np - new petrol station in optimal solution
26+
27+
# SOLUTION
128
```
229
import heapq
330
431
def place_stations(n_new_stations, length_of_road, stations):
5-
heap = create_heap_with(stations, length_of_road)
32+
heap = create_max_heap_with(stations, length_of_road)
633
n_stations_placed = 0
7-
while len(heap) != 0 and n_new_stations != n_stations_placed:
34+
while len(heap) != 0 and n_new_stations > 0:
835
longest_road = heapq.heappop(heap)
36+
print longest_road
937
new_road = place_station_on_road(longest_road)
1038
if new_road[0] != 0: # check distance between next station
1139
heapq.heappush(heap, new_road)
1240
n_new_stations -= 1
1341
n_stations_placed += 1
1442
return n_stations_placed
1543
16-
def create_heap_with(stations, length_of_road):
44+
def create_max_heap_with(stations, length_of_road):
1745
heap = list()
1846
sorted_station = stations.sort()
1947
distances = list()
@@ -28,7 +56,7 @@ def create_heap_with(stations, length_of_road):
2856
distances.append(len_of_road)
2957
for len_of_road in distances:
3058
# distances between stations, n_stations_on_road, total length of road
31-
node = (len_of_road, 0, len_of_road)
59+
node = (-len_of_road, 0, len_of_road)
3260
heapq.heappush(heap, node)
3361
return heap
3462
@@ -40,13 +68,12 @@ def place_station_on_road(road):
4068
if n_stations_on_road >= length_of_road:
4169
return (0, n_stations_on_road, length_of_road)
4270
distance_between = float(n_stations_on_road) / float(length_of_road)
43-
node = (distance_between, n_stations_on_road, length_of_road)
71+
node = (-distance_between, n_stations_on_road, length_of_road)
4472
return node
4573
46-
4774
M = 5
4875
K = 3
49-
T = [0,1,2,4]
76+
T = [0,1,2,3,4]
5077
5178
print place_stations(K, M, T)
5279
```

0 commit comments

Comments
 (0)