A town is divided into a rectangular grid of neighborhoods. Some neighborhoods contain fire stations.
In one minute, a fire truck can move to any horizontally, vertically, or diagonally neighboring cell. Therefore, the travel time between neighborhoods (firstRow, firstColumn) and (secondRow, secondColumn) is:
max(abs(firstRow - secondRow), abs(firstColumn - secondColumn))
1 represents a neighborhood with a fire station.0 represents a neighborhood without a fire station.The response time for a neighborhood is its travel time from the nearest fire station. The town's response time is the largest response time among all neighborhoods.
The town may build one additional fire station by changing at most one 0 to 1. Return the smallest possible response time for the town.
int minimumFireResponseTime(List<List<Integer>> townMap)
townMap represents the neighborhoods of the town.0.0.1 ≤ townMap.size() ≤ 5001 ≤ townMap.get(i).size() ≤ 500townMap.get(i).get(j) is either 0 or 1. minimumFireResponseTime( townMap = List.of( List.of(1, 0, 0), List.of(0, 0, 0), List.of(0, 0, 0) ))
Output: 1
Build the new fire station at neighborhood (1, 1). Every neighborhood is then at most one minute from a fire station.
minimumFireResponseTime( townMap = List.of( List.of(0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0), List.of(0, 0, 0, 0, 0) ))
Output: 2
Build a fire station at neighborhood (2, 2). The farthest neighborhoods are two minutes away.
minimumFireResponseTime( townMap = List.of( List.of(1, 1, 1), List.of(1, 1, 1) ))
Output: 0
Every neighborhood already contains a fire station, so every response time is zero.