FastPrepTV Tower Expansion

TV Tower Expansion

Wells Fargo logoWells Fargo● MediumFULLTIMEOA
Learn

Problem statement

You are given a rectangular binary matrix city. A cell with value 1 initially contains a TV tower, and a cell with value 0 does not.

Each day, every cell containing a tower installs towers in its orthogonally adjacent cells: up, down, left, and right, when those cells are inside the matrix.

Return the number of days required for every cell to contain a tower. If the city has no initial tower, return -1.

Function

daysToCoverCity(city: int[][]) → int

Examples

Example 1

city = [[1,0,0],[0,0,0]]return = 3

The farthest cell is three orthogonal steps from the initial tower.

Example 2

city = [[1,0,1],[0,0,0],[0,1,0]]return = 1

Every empty cell is adjacent to at least one of the three initial towers.

Example 3

city = [[1,1],[1,1]]return = 0

The city is already fully covered.

Example 4

city = [[0,0],[0,0]]return = -1

No initial tower exists, so expansion cannot begin.

Constraints

  • 1 <= city.length <= 500.
  • 1 <= city[i].length <= 500.
  • Every row has the same length.
  • city[i][j] is 0 or 1.

More Wells Fargo problems

See Wells Fargo hiring insights
public int daysToCoverCity(int[][] city) {
  // write your code here
}
city[[1,0,0],[0,0,0]]
expected3
Checking account…