FastPrepMaximum Survival Time in a Graph Pursuit

Maximum Survival Time in a Graph Pursuit

Zip logoZip● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

An undirected connected graph has roomCount rooms numbered from 0 through roomCount - 1. Tom starts in tomStart and Jerry starts in jerryStart.

For this exercise, a room is reachable safely by Jerry when Jerry's shortest-path distance to that room is no greater than Tom's shortest-path distance. Return the greatest Jerry distance among all such rooms. This is the maximum number of whole escape moves supported by the reported distance comparison.

Function

maxEscapeSeconds(roomCount: int, edges: int[][], tomStart: int, jerryStart: int) → int

Examples

Example 1

roomCount = 4edges = [[0,1],[1,2],[2,3]]tomStart = 0jerryStart = 2return = 1

Jerry can safely reach room 3 in one move; farther safe progress is impossible.

Example 2

roomCount = 4edges = [[0,1],[0,2],[0,3]]tomStart = 0jerryStart = 1return = 0

Every other leaf takes Jerry two steps but Tom one, so only Jerry's start is safe.

Example 3

roomCount = 6edges = [[0,1],[1,2],[2,3],[3,4],[4,5],[2,5]]tomStart = 0jerryStart = 4return = 2

Jerry can move two edges to a room reached no later than Tom.

Constraints

  • 2 <= roomCount <= 10^5.
  • The graph is simple, undirected, and connected.
  • tomStart != jerryStart.

More Zip problems

See Zip hiring insights
public int maxEscapeSeconds(int roomCount, int[][] edges, int tomStart, int jerryStart) {
    // Write your solution here.
}
roomCount4
edges[[0,1],[1,2],[2,3]]
tomStart0
jerryStart2
expected1
Checking account…