Laying a Pipe Over Uneven Ground

Implement gentlestPipe

A water pipe has to be laid over uneven ground described by a grid of heights, from the pump at the top-left cell to the tank at the bottom-right cell, moving between side-neighbouring cells. The strain of a route is the largest height difference between two consecutive cells on it. Find the smallest possible strain.

If steps up to some limit are allowed, deciding whether the destination is reachable is an ordinary flood fill. Because a larger limit never hurts, the smallest workable limit can be found by binary search.

Example 1:

Input: heights = [[2,8,9,3],[5,3,6,8],[4,7,5,2]]

Output: 3

Example 2:

Input: heights = [[7]]

Output: 0

Example 3:

Input: heights = [[1,50],[30,60]]

Output: 30

+ 14 hidden test cases run on Submit.

Constraints:

  • ●1 ≤ rows, cols ≤ 8; 0 ≤ heights[r][c] ≤ 99
  • ●A pipe starts at the pump on the top-left cell and must end at the tank on the bottom-right cell, passing from each cell to a side-neighbouring cell (up, down, left or right)
  • ●The strain of a pipe route is the LARGEST absolute height difference between two consecutive cells on it
  • ●Return the smallest possible strain over all pipe routes (0 for a single cell)

heights =

[[2,8,9,3], [5,3,6,8], [4,7,5,2]]