1970. Last Day Where You Can Still Cross

Practice Last Day Where You Can Still Cross for free on NudgeCode: Hard, Binary Search pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

A row x col grid starts as all land. On day i (1-indexed), the cell cells[i-1] = [r, c] (1-indexed coordinates) floods and becomes water forever.

You want to walk from any cell in the top row to any cell in the bottom row, moving up, down, left or right and only on land. Return the last day on which this is still possible.

Example

row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]  ->  2
row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]  ->  3

Constraints