Last Day Where You Can Still Cross - Problem

Imagine you're standing at the edge of a vast island that's slowly being consumed by rising floodwaters. Every day, more land disappears beneath the waves, and you need to find the last possible day you can still walk from the northern shore to the southern shore.

You start with a row ร— col grid representing your island, where initially every cell is land (0). Each day, exactly one cell becomes flooded with water (1) according to a predetermined schedule given in the cells array.

Your goal is to determine the last day when you can still find a path from any cell in the top row to any cell in the bottom row, walking only on land cells and moving in the four cardinal directions (up, down, left, right).

Input: Three parameters - row (number of rows), col (number of columns), and cells (a 1-indexed array where cells[i] = [r, c] means cell at row r, column c floods on day i).

Output: Return the last day number when crossing from top to bottom is still possible.

Input & Output

example_1.py โ€” Basic Island
$ Input: row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
โ€บ Output: 2
๐Ÿ’ก Note: Day 0: All land, can cross. Day 1: (1,1) floods, can still cross via (1,2)โ†’(2,2). Day 2: (2,1) floods, can still cross via (1,2)โ†’(2,2). Day 3: (1,2) floods, cannot reach bottom row. Answer: Day 2 is the last day we can cross.
example_2.py โ€” Narrow Path
$ Input: row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]
โ€บ Output: 1
๐Ÿ’ก Note: Day 0: All land. Day 1: (1,1) floods, can cross via (1,2)โ†’(2,2). Day 2: (1,2) floods, cannot reach any bottom cell. Answer: Day 1 is the last day.
example_3.py โ€” Larger Grid
$ Input: row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,1],[3,2]]
โ€บ Output: 3
๐Ÿ’ก Note: After 3 days of flooding, there's still a path from top row to bottom row. After day 4, when (2,2) floods, the path gets blocked. So day 3 is the last valid day.

Constraints

  • 2 โ‰ค row, col โ‰ค 2 ร— 104
  • 4 โ‰ค row ร— col โ‰ค 2 ร— 104
  • cells.length == row ร— col
  • 1 โ‰ค ri โ‰ค row
  • 1 โ‰ค ci โ‰ค col
  • All the values of cells are unique

Visualization

Tap to expand
Island Flooding SimulationDay 0Multiple PathsDay 3Path ExistsDay 5Last PathDay 6No Path!Binary Search ProcessDays 0-5: Can CrossDay 5: AnswerDays 6+: Cannot CrossBinary search finds day 5 as the last valid crossing day๐ŸŽฏ Key Insight: Use monotonic property for binary search optimization
Understanding the Visualization
1
Day 0: Virgin Island
All cells are land, multiple paths exist from north to south
2
Early Days: Partial Flooding
Some cells flood but alternative paths remain available
3
Critical Day: Last Crossing
Final day when at least one path still connects top to bottom
4
Cutoff Day: No Path
Flooding has severed all connections between north and south shores
Key Takeaway
๐ŸŽฏ Key Insight: Since flooding is irreversible, the ability to cross follows a monotonic pattern - if day X blocks crossing, all days after X also block it. This makes binary search the perfect optimization technique!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen