Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Last Day Where You Can Still Cross

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1970" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively. Initially on day 0, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array cells, where cells[i] = [ri, ci] represents that on the ith day, the cell on the rith row and cith column (1-based coordinates) will be covered with water (i.e., changed to 1). You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the four cardinal directions (left, right, up, and down). Return the last day where it is possible to walk from the top to the bottom by only walking on land cells. Example 1: Input: row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]] Output: 2 Explanation: The above image depicts how the matrix changes each day starting from day 0. The last day where it is possible to cross from top to bottom is on day 2. Example 2: Input: row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]] Output: 1 Explanation: The above image depicts how the matrix changes each day starting from day 0. The last day where it is possible to cross from top to bottom is on day 1. Example 3: Input: row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]] Output: 3 Explanation: The above image depicts how the matrix changes each day starting from day 0. The last day where it is possible to cross from top to bottom is on day 3. 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.

Explanation

Here's a breakdown of the solution approach, followed by the Python code:

  • Binary Search for the Last Day: Perform a binary search on the range of days (1 to row * col) to find the last day where a path exists from top to bottom.

  • Check Path Existence with DFS/BFS: For each day in the binary search, simulate the flooded matrix up to that day and use Depth-First Search (DFS) or Breadth-First Search (BFS) to check if a path exists from the top row to the bottom row using only land cells (0s).

  • Early Termination: If no path is found for a given day, it means the last possible day is earlier. Adjust the binary search range accordingly.

  • Runtime Complexity: O(row col log(row col)), due to the binary search and the DFS/BFS in each iteration. Storage Complexity: O(row col), for the matrix representation and DFS/BFS queue.

Code

    def latestDayToCross(row: int, col: int, cells: list[list[int]]) -> int:
    """
    Finds the last day that it is possible to walk from the top to the bottom by only walking on land cells.
    """

    def can_walk(day: int) -> bool:
        """
        Checks if a path exists from the top to the bottom on the given day.
        """
        grid = [[0] * col for _ in range(row)]
        for i in range(day):
            r, c = cells[i]
            grid[r - 1][c - 1] = 1  # Mark as water

        def dfs(r: int, c: int) -> bool:
            """
            Performs Depth-First Search to check if a path exists.
            """
            if r < 0 or r >= row or c < 0 or c >= col or grid[r][c] == 1:
                return False
            if r == row - 1:
                return True

            grid[r][c] = 1  # Mark as visited

            return (
                dfs(r + 1, c)
                or dfs(r - 1, c)
                or dfs(r, c + 1)
                or dfs(r, c - 1)
            )

        for c in range(col):
            if grid[0][c] == 0:
                if dfs(0, c):
                    return True

        return False

    left, right = 1, row * col
    ans = 0

    while left <= right:
        mid = (left + right) // 2
        if can_walk(mid):
            ans = mid
            left = mid + 1
        else:
            right = mid - 1

    return ans

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Last Day Where You Can Still Cross