FastPrepMinimum Teleporter Repairs

Minimum Teleporter Repairs

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Countries have directed teleport connections. Some countries have a broken departure teleporter, which takes one day to repair before it can be used. Find the minimum number of repair days needed to travel from source to destination; teleporting itself takes zero days.

For this exercise, assume countries are numbered 0..n-1, and each [a,b] connection permits travel from a to b. A broken teleporter belongs to its departure country, regardless of which outgoing connection is chosen. Arrival at a broken destination does not require repair. A teleporter stays repaired after its first repair; an optimal route can be chosen without repeated countries.

For this exercise, return -1 if no directed route exists and 0 if the endpoints coincide. Repeated edges and repeated entries in broken do not change the answer.

Function

minTeleporterRepairs(n: int, connections: int[][], broken: int[], source: int, destination: int) → int

Examples

Example 1

n = 7connections = [[0,1],[0,2],[0,3],[1,2],[1,5],[2,3],[2,4],[2,5],[3,4],[3,6],[5,6]]broken = [5]source = 1destination = 6return = 0

The reported path 1 -> 2 -> 3 -> 6 avoids departing from the broken teleporter at 5.

Example 2

n = 7connections = [[0,1],[0,2],[0,3],[1,2],[1,5],[2,3],[2,4],[2,5],[3,4],[3,6],[5,6]]broken = [1,5]source = 1destination = 6return = 1

Every nontrivial route departs from the broken source at 1, but 1 -> 2 -> 3 -> 6 requires no additional repair.

Example 3

n = 2connections = [[0,1]]broken = []source = 1destination = 0return = -1

There is no directed route from country 1 back to country 0.

Constraints

  • For this exercise, assume 1 <= n <= 2000 and at most 10000 directed connections.
  • Every country index, including endpoints and broken entries, lies in [0,n-1].
  • broken.length <= 2000; repeated entries and self-connections are allowed.

More Google problems

See Google hiring insights
public int minTeleporterRepairs(int n, int[][] connections, int[] broken, int source, int destination) {
    // Write your code here
}
n7
connections[[0,1],[0,2],[0,3],[1,2],[1,5],[2,3],[2,4],[2,5],[3,4],[3,6],[5,6]]
broken[5]
source1
destination6
expected0
Checking account…