Last Day Where You Can Still Cross - Complete Solution Guide
Last Day Where You Can Still Cross is LeetCode problem 1970, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in Python3, Java, C++, C.
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] = [r i , c i ] represents that on the i th day, the cell on the r i th row and c i th column ( 1-based coordinates) will be covered with water (i.e.
Detailed Explanation
The problem describes a grid that initially consists entirely of land (represented by 0). Each day, a specific cell turns into water (represented by 1). The goal is to find the last day on which it is still possible to traverse from the top row to the bottom row by only stepping on land cells, moving only up, down, left, or right.
Solution Approach
The solution employs a Union Find data structure and iterates backward through the 'cells' array. For each day, it converts the cell to land and connects it to its adjacent land cells using Union Find. It also connects the top row cells to a virtual 'top' node and the bottom row cells to a virtual 'bottom' node. The algorithm stops when the 'top' and 'bottom' nodes become connected, indicating the first day crossing is impossible. The previous day is therefore the last day a crossing was possible.
Step-by-Step Algorithm
- Step 1: Initialize a Union Find data structure with `row * col + 2` nodes. The first `row * col` nodes represent the grid cells, one node represents the virtual 'top', and one node represents the virtual 'bottom'.
- Step 2: Initialize a grid with all cells as water (0).
- Step 3: Iterate through the 'cells' array in reverse order (from the last day to the first).
- Step 4: For each day, mark the corresponding cell in the grid as land (1).
- Step 5: Connect the current land cell to its adjacent land cells (up, down, left, right) using the Union Find 'union' operation.
- Step 6: If the current land cell is in the first row, connect it to the virtual 'top' node.
- Step 7: If the current land cell is in the last row, connect it to the virtual 'bottom' node.
- Step 8: Check if the virtual 'top' and 'bottom' nodes are connected using the Union Find 'find' operation. If they are connected, it means a path exists from the top row to the bottom row, and the current day is the first day crossing is impossible. Return the index `i` of the current day.
- Step 9: If the loop finishes without finding a day where the 'top' and 'bottom' nodes are connected, return 0 (this handles cases where crossing is never possible, even on day 0).
Key Insights
- Insight 1: The problem can be solved efficiently by working backward from the last day. This allows us to treat the problem as finding the *first* day on which crossing is *impossible*.
- Insight 2: Union Find is a suitable data structure to keep track of connected land cells. It allows us to efficiently determine if the top row and bottom row are connected by land.
- Insight 3: Adding virtual top and bottom nodes simplifies the connectivity check. We connect all cells in the first row to the top node and all cells in the last row to the bottom node. A path from the top to the bottom exists if the top and bottom nodes are in the same connected component.
Complexity Analysis
Time Complexity: O(R * C * alpha(R * C))
Space Complexity: O(R * C)
Topics
This problem involves: Array, Binary Search, Depth-First Search, Breadth-First Search, Union Find, Matrix.
Companies
Asked at: Atlassian.