Skip to content
BytePatterns

Rising Tide Crossing

HardGraphs#union-find#minimum-spanning-tree#sorting~45m

Problem

A flooded field is a grid of ground heights, and the water level rises steadily from 0. A rescue boat can float over any cell whose ground height is at most the current water level, and it moves up, down, left or right between floatable cells. Return the lowest water level at which the boat can travel from the top-left cell to the bottom-right cell. The grid has at least one cell, and heights are whole numbers from 0 upward.

Examples

Input:  heights = [[1, 5, 2],
                   [2, 9, 1],
                   [3, 4, 1]]
Output: 4
Why:    down the left edge and along the bottom, the highest ground is 4
Input:  heights = [[0, 2],
                   [1, 3]]
Output: 3
Why:    the destination itself sits at height 3
Input:  heights = [[7]]
Output: 7
Why:    edge case, start and finish are the same cell

Hints

0 / 3

Stuck on the idea rather than the code? Kruskal's Spanning Tree covers it.