Problem
Ask
Submissions

Problem: Swim in Rising Water

Medium
30 min
Explore how to solve the Swim in Rising Water problem by understanding matrix traversal and elevation-based movement constraints. Learn to find the minimum time to swim across an n by n grid as water levels rise, applying efficient strategies for coding interviews.

Statement

Given an n×nn \times n grid (2D matrix) where each cell grid[i][j] represents the elevation at position (i, j).

Once it starts to rain, the water level rises over time. At any given time t, the water depth across the grid equals t. A swimmer can move from one cell to an adjacent cell (up, down, left, or right) if both cells have elevations less than or equal to the current water level t.

If the elevation condition is satisfied, a swimmer can swim any distance instantly. However, he cannot move outside the grid boundaries.

Return the minimum time t at which it becomes possible to swim from the top-left cell (0,0)(0, 0) to the bottom-right cell (n1,n1)(n - 1, n - 1).

Constraints:

  • n==n == grid.length

  • n==n == grid[i].length

  • 11 \leq nn 30\leq 30

  • 00 \leq grid[i][j] < n2< n^2

  • Each value grid[i][j] is unique.

Problem
Ask
Submissions

Problem: Swim in Rising Water

Medium
30 min
Explore how to solve the Swim in Rising Water problem by understanding matrix traversal and elevation-based movement constraints. Learn to find the minimum time to swim across an n by n grid as water levels rise, applying efficient strategies for coding interviews.

Statement

Given an n×nn \times n grid (2D matrix) where each cell grid[i][j] represents the elevation at position (i, j).

Once it starts to rain, the water level rises over time. At any given time t, the water depth across the grid equals t. A swimmer can move from one cell to an adjacent cell (up, down, left, or right) if both cells have elevations less than or equal to the current water level t.

If the elevation condition is satisfied, a swimmer can swim any distance instantly. However, he cannot move outside the grid boundaries.

Return the minimum time t at which it becomes possible to swim from the top-left cell (0,0)(0, 0) to the bottom-right cell (n1,n1)(n - 1, n - 1).

Constraints:

  • n==n == grid.length

  • n==n == grid[i].length

  • 11 \leq nn 30\leq 30

  • 00 \leq grid[i][j] < n2< n^2

  • Each value grid[i][j] is unique.