Skip to content

Commit fa01ca2

Browse files
author
Joseph Luce
authored
Update Minimize_road_between_stations.md
1 parent 129a42d commit fa01ca2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

real_interview_questions/Google/Minimize_road_between_stations.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ Fn - finish of motorway
2525
Ps - pre-existing petrol station
2626
Np - new petrol station in optimal solution
2727

28-
# SOLUTION
28+
# EXPLAINATION
29+
The question want us to figure out which sections of roads should we place a new station given k stations. By using a max heap, we can figure out the longest road we currently have and place a station on it. However, to get the sections of roads, we must first sort the array and iterate it to get those sections. Then we can build our max heap with it.
30+
31+
However, there are a few edge cases to consider. We want to avoid evaluating sections of roads that have no length. Also what happens when we revisit the same section of road? If that happens, we need to restart the process of replacing the stations on that road if there were any. So we need to keep a few key peices of information. The total length of this section of road, the current number of stations on this road and the distance between each station on this road. The distance between each station on this road will be used to sort the max heap.
32+
33+
# SOLUTION 1
2934
```
3035
import heapq
3136

0 commit comments

Comments
 (0)