Ctrl K

Last Dry Crossing Day

Hard
A field has rows rows and cols columns, both numbered from 1. On day 0 the whole field is dry land. After that, one cell goes under water each day: on day i (counting from 1) the cell flooded[i - 1] = [r, c] floods, meaning row r, column c. Every cell floods exactly once. A traveller wants to walk on dry land from any cell of the top row to any cell of the bottom row, moving only up, down, left or right. Return the last day such that, once that day's cell has flooded, the walk is still possible.
Example 1
rows=2, cols=3, flooded=[[1, 2], [2, 1], [2, 3], [1, 1], [2, 2], [1, 3]] → 2

After day 2 the dry cells are (1,1), (1,3), (2,2), (2,3), and you can step down from (1,3) to (2,3). On day 3 (2,3) floods and no path is left. The answer is 2.

Example 2
rows=2, cols=2, flooded=[[1, 1], [1, 2], [2, 1], [2, 2]] → 1

By day 2 the whole top row is under water, so the last dry day is day 1.

Constraints
  • 2 ≤ rows, cols ≤ 2 · 10⁴
  • 4 ≤ rows · cols ≤ 2 · 10⁴
  • len(flooded) == rows · cols
  • 1 ≤ flooded[i][0] ≤ rows, 1 ≤ flooded[i][1] ≤ cols
  • All cells in flooded are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCrossing at High Tide HardRoute With the Highest Bottleneck MediumThe Gentlest Climb MediumFrame Around an Ink Blot HardFill a Single Cell Hard
loading Python…
Input
rows=2, cols=3, flooded=[[1, 2], [2, 1], [2, 3], [1, 1], [2, 2], [1, 3]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 37 tests on the server, hidden ones included.