LeetCode 1970 Solution Explained Step by Step (Binary Search + BFS)

At first glance, LeetCode 1970 looks intimidating. Thereโ€™s a grid, flooding cells day by day, and somehow youโ€™re supposed to figure out the last possible day you can still cross from the top to the bottom.

If your first thought was โ€œDo I simulate every day?โ€ โ€” youโ€™re not alone.
Letโ€™s slow it down and solve this step by step, in a way that actually makes sense.


๐Ÿง  Understanding the Problem (In Plain English)

Youโ€™re given:

  • A grid with row x col cells
  • A list cells, where each entry tells you which cell gets flooded on a given day

Rules:

  • On day 1, the first cell floods
  • On day 2, the second cell floods
  • And so onโ€ฆ

Your goal:

Find the last day when you can still walk from the top row to the bottom row, moving only through unflooded cells.

You can move:

  • Up, down, left, right (no diagonals)

โŒ The Naive Idea (And Why It Fails)

A brute-force approach would be:

  1. Simulate flooding day by day
  2. After each day, check if a path exists

But checking paths every day is expensive:

  • Grid size can be up to 2ร—10โด cells
  • Path search (BFS/DFS) every day = too slow

So we need something smarter.


๐Ÿ’ก Key Insight: Think Backwards

Instead of asking:

โ€œOn which day does crossing stop being possible?โ€

Ask:

โ€œIf I start from the last day and undo flooding, when does crossing become possible again?โ€

This small change makes a huge difference.


๐Ÿš€ Strategy That Actually Works

We combine Binary Search + BFS.

Why Binary Search?

  • The answer is a day number
  • If you can cross on day d, you can also cross on any earlier day
  • That means the condition is monotonic
  • Perfect use case for binary search

๐Ÿงช Step-by-Step Approach

Step 1: Binary Search on Days

We search between:

  • left = 1
  • right = row * col

For a given day mid, we ask:

โ€œIs it possible to cross on this day?โ€


Step 2: Check if Crossing Is Possible (BFS)

For day mid:

  1. Mark the first mid cells as flooded
  2. Start BFS from all unflooded cells in the top row
  3. Try to reach the bottom row

If we reach the bottom โ†’ crossing is possible
If not โ†’ crossing is impossible


โœ… Final Solution Code (Python)

from collections import deque

class Solution:
    def latestDayToCross(self, row: int, col: int, cells: list[list[int]]) -> int:
        
        def canCross(day):
            grid = [[0] * col for _ in range(row)]
            
            # Flood cells up to given day
            for i in range(day):
                r, c = cells[i]
                grid[r - 1][c - 1] = 1
            
            queue = deque()
            visited = [[False] * col for _ in range(row)]
            
            # Start BFS from top row
            for c in range(col):
                if grid[0][c] == 0:
                    queue.append((0, c))
                    visited[0][c] = True
            
            directions = [(1,0), (-1,0), (0,1), (0,-1)]
            
            while queue:
                r, c = queue.popleft()
                if r == row - 1:
                    return True
                
                for dr, dc in directions:
                    nr, nc = r + dr, c + dc
                    if 0 <= nr < row and 0 <= nc < col:
                        if not visited[nr][nc] and grid[nr][nc] == 0:
                            visited[nr][nc] = True
                            queue.append((nr, nc))
            
            return False
        
        left, right = 1, row * col
        answer = 0
        
        while left <= right:
            mid = (left + right) // 2
            if canCross(mid):
                answer = mid
                left = mid + 1
            else:
                right = mid - 1
        
        return answer

โฑ Time & Space Complexity

Time Complexity

  • Binary Search: O(log(row * col))
  • BFS per check: O(row * col)
  • Total: O(row * col * log(row * col))

Space Complexity

  • Grid + visited matrix: O(row * col)

Efficient enough for large inputs.


๐Ÿงฉ Why This Solution Feels Smart

  • You avoid checking every day
  • You only run BFS ~log(n) times
  • The problem becomes manageable instead of overwhelming

This is a classic example of:

Reframing the problem โ†’ simpler solution


๐Ÿ Final Thoughts

LeetCode 1970 isnโ€™t really about grids or flooding.
Itโ€™s about recognizing monotonic behavior and combining:

  • Binary Search
  • Graph traversal (BFS)

Once that clicks, the problem goes from โ€œhardโ€ to โ€œohโ€ฆ thatโ€™s clever.โ€