542. Swim in Rising Water

HardBinary SearchBinary SearchBFSDFS

You are given an n x n grid where grid[i][j] is the elevation at that cell. At time t, the water level is t and you can swim between adjacent cells (up/down/left/right) only if both cells have elevation at most t. Starting at the top-left, return the least time t to reach the bottom-right. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the minimum time to reach the bottom-right.

Examples

Example 1
Input: {"grid":[[0,2],[1,3]]}
Output: 3
Explanation: Need water level 3 to reach the corner.
Example 2
Input: {"grid":[[0]]}
Output: 0
Explanation: Already at the destination.

Constraints

Asked by

MetaGoogleAmazonMicrosoftBloomberg
Solve this problem in the editor →