Last Day Where You Can Still Cross
The problem “Last Day Where You Can Still Cross” has an approximate isomorphic problem: “Surrounded Regions”.
In both problems, you are exploring a grid and traversing through available paths.
“Surrounded Regions” is simpler as you just need to identify regions that are completely surrounded by ‘X’ in a grid of ‘X’ and ‘O’.
“Last Day Where You Can Still Cross” is more complex because you need to additionally consider the time aspect (represented by day) and determine the last day where a crossing is still possible. The ‘crossing’ concept in “Last Day Where You Can Still Cross” is analogous to finding an open region in “Surrounded Regions”.
Both require using depthfirst search or breadthfirst search techniques to traverse the grid, though “Last Day Where You Can Still Cross” also requires binary search due to the time component.


Problem Classification
This is a Gridbased and TimeEvolving Path Finding problem in the domain of Graph Theory and Dynamic Programming.
Here’s the breakdown:
Gridbased: The problem is based on a 2dimensional grid (or matrix) where each cell represents a piece of land (0) or water (1).
TimeEvolving: The state of the grid changes over time, with certain cells becoming flooded (changing from land to water) each day.
Path Finding: The task is to find if there exists a path (only moving in the four cardinal directions) from the top row to the bottom row of the grid.
The ‘What’ components are:
Matrix (Grid): A 2dimensional grid which starts with all cells representing land (0) and some cells turn into water (1) as time progresses.
Time: The grid evolves over time with each cell turning into water on a specific day mentioned in the cells array.
Cells array: The array gives information about which cell becomes water on which day.
Path: A path is a series of steps from the top to the bottom row only moving in four cardinal directions on the grid.
Output: The last day on which it is possible to find a path from the top row to the bottom row using only land cells.
It can be further classified as a problem of Graph Theory as the grid can be viewed as a graph where each cell is a node and each node is connected to its neighbors. It also involves elements of Dynamic Programming as it may require us to keep track of the changing state of the grid and find optimal paths through it.
Problem Simplification and Explanation
The problem is similar to a game of “The Floor is Lava” on a grid of squares. Initially, all squares are safe to step on. Each day, however, one of the squares turns into ’lava’ (water in our problem) and cannot be stepped on anymore. The grid represents your game field and each square on it is either ‘safe’ (land) or ’lava’ (water).
Your task is to get from one side of the game field (the top row of the grid) to the other side (the bottom row of the grid) by only stepping on safe squares (land). However, with each passing day, more squares become ’lava’, making your task harder.
The order in which the squares become lava is given by the ‘cells’ list  each pair of numbers in this list represents the coordinates of a square that will turn into lava on that day (with the day number being the index of the pair in the list).
You are to determine the last day on which it is still possible to cross from one side of the field to the other. This is akin to finding out the last day on which you can still play the game and successfully cross the field without stepping on the lava.
The key concepts here are the representation of the game field as a grid, the evolution of the state of this grid over time, and the task of finding a safe path across this grid. These concepts interact as you are essentially tracing a safe path across an everchanging game field.
Constraints
Gridbased problem: This problem is based on a grid or a matrix. This could allow for the use of techniques associated with 2D arrays or matrices such as depthfirst search (DFS) or breadthfirst search (BFS).
Timesequence of grid changes: The problem provides an ordered list of how and when the grid changes. This could allow us to check the connectivity from the top row to the bottom row at every step and stop once the connectivity is lost. This saves us from unnecessary computations for the rest of the days.
Cell transitions are one way (Land to Water): Once a cell becomes water, it remains as water. This allows us to progressively update our grid, without needing to account for changes back to land.
Row and Column ranges: The given constraints for the row and column sizes (2 <= row, col <= 2 * 10^4) are moderate. The range of these sizes may allow us to consider an algorithm that runs in polynomial time without exceeding time limits.
Number of cells and their uniqueness: The length of cells is equal to the total number of cells in the matrix, and all values are unique. This ensures every cell will eventually become water. Thus, it’s guaranteed the traversal from top to bottom will become impossible at some point, and there’s no need to consider cases where the land route remains open indefinitely.
Goal of the problem: We are trying to find the “last day where it is possible to walk from the top to the bottom by only walking on land cells”. This means we are interested in the state just before the grid became fully untraversable from top to bottom. It suggests we need a way to check the connectivity from the top row to the bottom row after each update.
These specific characteristics and patterns can inform an efficient strategy to solve this problem by limiting the scope of the computation to be performed and helping to design an algorithm that can provide the solution more effectively.
Thought Process
Here’s the thought process stepbystep:
Understand the Problem Statement: We are given a grid, which is initially all land. Each day, some land cells will be covered with water. Our goal is to find the last day that it is possible to walk from the top row to the bottom row only walking on land cells.
Identify Key Aspects: A key aspect here is to keep track of the state of the grid each day and check the connectivity from the top row to the bottom row. We can see the problem as a graph connectivity problem, where each cell is a node and neighboring cells are connected.
Plan an Approach: To solve the problem efficiently, we can take a reverse approach: consider the grid as initially all water, and land cells emerge each day in reverse order. We will use a unionfind data structure to keep track of the connectivity and a priority queue to handle cells in reverse flooding order.
Define the Data Structure: We need to define a unionfind data structure which can perform union and find operations. Union operation connects two nodes, and Find operation determines the root of a node.
Implement the Approach: Implement the unionfind structure, handle the cells in reverse flooding order, and keep track of the last day that it is possible to walk from the top row to the bottom row only walking on land cells.
Let’s put these steps into code:


This code first initializes the grid and unionfind structure. It then starts from the last day and for each day, marks the corresponding cell as land and performs union operation for its adjacent land cells. If the top row and the bottom row are connected, it returns the current day. The time complexity is approximately O(row * col * α(row * col)), where α is the InverseAckermann function, which grows very slowly and can be considered almost constant for all practical purposes. So, the time complexity can be considered almost linear with respect to the grid size.
Solution Approach and Analysis
Given the problem statement, here’s how I would approach solving it:
Initialization: Initialize a 2D grid or matrix with ‘0’ (representing land) based on the provided row and col sizes. Also, prepare a list to store the days when it’s still possible to walk from top to bottom.
Flood Simulation: The cells array provides the sequence of flooding. Traverse through the cells array and for each cell at [ri, ci], change the corresponding cell in the grid to ‘1’ (representing water). This is like playing a timelapse of how the land progressively becomes flooded with water, day by day.
Connectivity Check: After each update to the grid, check if it’s still possible to walk from any cell in the top row to any cell in the bottom row by only stepping on land cells. We can achieve this by performing a DepthFirst Search (DFS) or BreadthFirst Search (BFS) from each land cell in the top row and see if we can reach the bottom row. This is similar to trying to walk from one end of a maze to the other, where the maze keeps getting flooded.
Record the Day: If we can still reach the bottom row, record the current day. If not, stop the process as we’re interested in the last day when the walk was possible.
Answer Extraction: The last recorded day would be our answer, i.e., the last day where it is possible to walk from the top to the bottom by only walking on land cells.
Metaphorically, think of this as trying to cross a forest (the matrix) from north to south while it’s getting flooded day by day. Each day, right after a part of the forest gets flooded (the cell becomes water), you check if you can still get to the other end through the remaining land. You keep track of the days when the journey was possible. The day after the last successful journey will be when the entire forest is inundated and it’s impossible to cross it, and that’s the day we’re trying to find.
Now, let’s walk through an example:
Let’s say row = 2
, col = 2
, and cells = [[1,1],[2,1],[1,2],[2,2]]
.
On day 0, the grid looks like:
0 0 0 0
On day 1, after flooding cell [1, 1], the grid becomes:
1 0 0 0
We can still cross from top to bottom, so record day 1.
On day 2, after flooding cell [2, 1], the grid becomes:
1 0 1 0
We can still cross from top to bottom, so record day 2.
On day 3, after flooding cell [1, 2], the grid becomes:
1 1 1 0
Now, it’s impossible to cross from top to bottom, so we stop and return the last recorded day, which is 2.
If the problem’s parameters change (e.g., row, col, or cells), it will directly affect the complexity of our solution. For example, larger row and col will increase the size of the grid and thus increase the complexity of DFS/BFS, and different cells array will change the sequence of flooding, affecting when the connectivity between the top and bottom is lost.
Identification of Applicable Theoretical Concepts
This problem can be tackled more efficiently by applying several concepts in computer science and graph theory:
Graph Traversal Algorithms: In this problem, the grid can be thought of as a graph where each cell is a node and neighboring cells are connected. Graph traversal algorithms such as DepthFirst Search (DFS) or BreadthFirst Search (BFS) are highly relevant here, as they can be used to check if it is still possible to cross from the top to the bottom.
UnionFind Data Structure: This problem involves a lot of operations related to the connectivity of nodes in a graph, which is where the unionfind data structure shines. Instead of performing DFS or BFS after every flood, we can maintain a unionfind set for the land cells and check the connectivity of toprow cells and bottomrow cells.
Disjoint Set Union (DSU) on Trees: This advanced data structure could also potentially be applied to optimize the connectivity checking. The idea would be to maintain multiple DSUs for different rows of cells and merge them when the water floods a new cell.
Binary Search: Instead of linearly checking each day to find the last day where it is possible to walk from the top to the bottom by only walking on land cells, binary search can be applied to find this day more efficiently, provided we have a fast enough way to check the connectivity for a specific day.
Sort and Priority Queue: If we consider the flood simulation from the reverse side, we can see it as land cells emerging from a fully flooded area. In this case, we can sort the cells by the flooding day in descending order and use a priority queue to handle the cells.
Graph Theory: The whole problem can be looked at as a problem of finding the critical path or bridge in a graph that, when removed, disconnects the graph.
By employing these concepts, the problem can be simplified and the efficiency of the solution can be significantly improved.
Coding Constructs
This code uses several problemsolving strategies including data modeling, the UnionFind data structure, and reverse problem solving.
This code is essentially trying to find the latest day a person can cross a river before it’s completely flooded. The river is represented as a grid, and each day, a cell in this grid gets filled with water. Starting from the last day and going backwards, the code checks if it’s possible to go from the top row to the bottom row through the unflooded cells.
The main logical elements used in this code include conditionals (
if
statements), loops (for
loop), array indexing, function calls (find
andunion
), and the concepts of classes and objects.The algorithmic approach used by this code can be described as follows:
 Represent the river as a 2D grid, with each cell having a day on which it gets flooded.
 Initialize an UnionFind data structure to manage the connections between cells.
 Start from the last day and go back to the first day.
 On each day, flood the corresponding cell and connect it to its neighboring cells that are not yet flooded.
 After each flooding, check if there’s still a path from the top row to the bottom row through the unflooded cells.
The key steps this code is performing on the input data include:
 Mapping each cell in the river to a day it gets flooded.
 Initializing the UnionFind data structure.
 Looping over the days in reverse order, flooding the corresponding cell and updating the UnionFind data structure.
 Checking if there’s a path from the top row to the bottom row after each day’s flooding.
The key algorithmic patterns used in this code include the UnionFind data structure for efficient union and connection checking, and the reverse problem solving strategy to determine the last possible day to cross the river. The grid is treated as a graph and the UnionFind structure allows efficient determination of whether two nodes (cells) in the graph are connected.
Language Agnostic Coding Drills
Dissection of the code into distinct coding concepts:
Variable Declaration and Initialization: Setting up variables and initializing them to their respective starting values.
Array Manipulation: Creating and manipulating arrays, such as the
grid
2D array and thedirs
array.Control Flow: Usage of
if
conditions, andfor
loops for controlling the flow of the program.Class and Object: The concept of creating classes and objects, and using methods within a class.
Data Structures: Usage of advanced data structures like a queue and UnionFind.
Functions: The concept of creating and calling functions, including recursive functions.
ObjectOriented Programming: Encapsulation of data and functions within a class, and instantiating objects from the class.
Listing of coding drills in increasing order of difficulty:
Variable Declaration and Initialization: This is a basic concept in every programming language and the first thing that beginners learn. Difficulty: Very Easy
Control Flow: Controlling the flow of the program with
if
conditions andfor
loops is also an early concept beginners learn. Difficulty: EasyArray Manipulation: Creating and manipulating arrays is a common operation, and it can involve more complexity depending on the operations being done. Difficulty: Easy to Medium
Functions: Creating and calling functions adds another layer of abstraction to the code, and it can get complex depending on the function’s purpose. Difficulty: Medium
Class and Object: The concept of classes and objects in objectoriented programming is a significant step up from procedural programming. It requires understanding of encapsulation and data/method grouping. Difficulty: Medium
ObjectOriented Programming: The concepts of objectoriented programming, including encapsulation, inheritance, and polymorphism, can be complex for beginners but are crucial for good software design. Difficulty: Medium to Hard
Data Structures: The usage of advanced data structures like UnionFind and queue, and understanding their underlying mechanisms, can be quite challenging. Difficulty: Hard
ProblemSolving Approach:
The problem statement is understood and variables are initialized to represent the given information.
A 2D array is created to represent the river grid and its state on each day.
An UnionFind data structure is created to manage connections between cells in the grid.
A loop is run from the last day to the first day. On each day, the corresponding cell is marked as flooded and it’s connected to its unflooded neighbors in the UnionFind data structure.
After each day’s flooding, a check is performed to see if there’s still a path from the top row to the bottom row through the unflooded cells. This check is done by seeing if the sets representing the top and bottom rows in the UnionFind structure are connected.
The last day where a path from top to bottom is found gives the solution to the problem.
Targeted Drills in Python
 Python Coding Drills:
Concept: Variable Declaration and Initialization


This simple drill shows how to declare and initialize variables.
Concept: Control Flow


This drill demonstrates how to use if
and else
statements to control program flow based on conditions.
Concept: Array Manipulation


This drill shows how to create an array (list in Python), add an element to the end, and remove a specific element.
Concept: Functions


This drill demonstrates how to declare a function, add_numbers, that takes two arguments and returns their sum.
Concept: Class and Object


This code creates an Animal class with an initializer and a method. It then creates an object of Animal and calls the method on it.
Concept: ObjectOriented Programming


This demonstrates inheritance in objectoriented programming, where the Dog class extends the Animal class and overrides the speak method.
Concept: Data Structures  Union Find


This drill shows how to implement a UnionFind data structure in Python and perform basic operations on it.
 Problemspecific Concepts:
Concept: Matrix and Coordinate Manipulation


This drill demonstrates how to create a 2D matrix, manipulate its values, and how to iterate through direction vectors which is necessary for the grid traversal in the problem.
 Assembly of the Solution:
 First, initialize the UnionFind object with the size of the grid.
 Then, create a 2D grid and fill it with initial values.
 Create a loop that runs
from the last day to the first, marking each cell as flooded each day.
 Inside this loop, after marking a cell, connect this cell to its unflooded neighbors in the UnionFind structure.
 After each Union operation, check if the top row is connected with the bottom row in the UnionFind structure.
 If they are connected, update the maximum number of days it’s possible to cross the river.
 Continue this process until the first day.
 At the end, return the maximum number of days it’s possible to cross the river. This gives the final solution.
Q&A
One possible solution is to do a Binary Search in a days space, however it gives TLE. Let’s invert the problem: find the first day from the end where there is a connection topbottom. Now,cells[day] is a new ground. We can use UnionFind to connect ground cells.
Yes, you are correct. The solution I provided above actually uses this inverted problem approach, by using a unionfind data structure to handle the connectivity of cells and traversing the cells in reverse order.
The idea behind this approach is to find the first day from the end where there is a connection from top to bottom. In this reversed scenario, cells become land each day instead of getting flooded. So, we start from the last day and for each day, mark the corresponding cell as land and perform union operations for its adjacent land cells. If the top row and the bottom row are connected, we return the current day, which is actually the last day it is possible to walk from the top to the bottom in the original problem.
Doing a binary search in the day space would indeed give a Time Limit Exceeded (TLE) error as it requires a check for each midday to see if it’s possible to walk from the top to the bottom, which results in a much higher time complexity. This problem requires an approach with a more efficient time complexity, like the unionfind approach, to avoid TLE.
Should have named the problem “Ecape the Matrix! " Missed a chance
That would indeed be a fun and appropriate name! This problem does bear some resemblance to navigating a matrix in search of an exit path. Good observation! It’s always good to bring a bit of humor and creativity into problem solving. This makes the process of learning and practicing programming more enjoyable. Happy coding!
If you couldn’t at first think of a binary search solution, consider Dijkstra’s algorithm using a maxheap. Starting from each cell from the top, look for the path with the highest (latest) possible day. The path’s maximum would be the minimum cell that you’ve visited while searching for the bottom row.
Yes, you’re correct. If it wasn’t immediately obvious to use binary search, Dijkstra’s algorithm with a priority queue (maxheap) could be a feasible alternative. This would involve creating a priority queue of cells, where the priority is the day on which the cell is flooded. You start from each cell on the top row and always pick the cell with the highest day from the queue. For each chosen cell, you try to relax its neighbours. The algorithm stops when you reach the bottom row. Then, you return the minimum day you’ve encountered during the path from the top to the bottom row.
The pseudocode would look something like this:


Please note that this approach can be less efficient than the binary search approach due to the usage of the priority queue and the additional relaxation step.
So Let’s analyse the question we can handle 1 based indexing. Lets see the naive approach using dfs As per the hint 1 we can use dfs to check wheather the path exists for row 0 by running the the dfs in the loop for every column
so for bruteforce we can run a loop from 0 to cells.size() and change land value to water for ith day and within that we can run a loop till column and check for every column and if the path exist then we will update our result.
So this was the naive approach but will it work!! No, it will give TLE. So how can we optimize our solution
Now let’s see the Hint 2 : it says we can use binary search what we can do is use binary search on cells matrix we will find the mid and changes our grid value to water till cells[mid] and run our check on every column if the path exists we will change our left = mid+1 update our result and if the path doesnot exist we will changes our right =mid1 and loop will run again. This way we reduced our first loop which we were using in the naive approach.
Your analysis of the problem and the potential solutions is quite accurate! The problem can indeed be solved using Depth First Search (DFS) in a brute force manner but, as you correctly noted, this would result in a Time Limit Exceeded (TLE) error for large inputs because the time complexity would be quite high. This approach would need to iterate through each cell for each day which would make it infeasible for large datasets.
The use of binary search to optimize this problem is an excellent insight. Binary search allows you to effectively reduce the search space and therefore significantly improve the time complexity of the solution. Instead of checking each day one by one, you divide the time into two halves and check only the midpoint day. Depending on whether a path exists or not on this day, you adjust your search to the appropriate half of the timeline. This approach has a time complexity of O(log n), which is a significant improvement over the brute force solution.
However, keep in mind that the implementation of this approach needs careful handling, especially for updating the grid state for each midpoint day check and for handling the 1based indexing. The use of a UnionFind data structure can help efficiently manage the grid state updates and check for path existence.
Here’s a highlevel idea of how the optimized solution can be implemented:
Sort the
cells
array based on the day of flooding, so you know the sequence of flooding.Use binary search on the days space, with left = 0 and right = cells.length  1.
For each midpoint day in the binary search:
Update the grid state up to the midpoint day.
Check if there is still a path from top to bottom. You can use a UnionFind data structure for this. If a path exists, update the result and move to the right half of the timeline (i.e.,
left = mid + 1
). Otherwise, move to the left half of the timeline (i.e.,right = mid  1
).
Return the last day where a path was found.
Note that for the grid state update and path check, you would need to correctly handle the 1based indexing as per the problem statement. This can involve subtracting 1 from the row and column indices.
Hope this helps!
A totally different way is done. The idea is to find a connected waterway from column 1 to column col. A waterway has 8 directions to cut off the land way. BSF using priority queue is the key point.
Your idea is quite interesting! In essence, you are suggesting to change the perspective and treat the problem as finding a “waterway” instead of a “land path”.
By considering the 8directional connectivity of water cells, you might be able to cut off the land path more effectively. For example, a corner cell (i.e., a cell that is connected to land cells both to its right and below) could effectively cut off two potential paths. By utilizing such cells early on, you might be able to find the last possible day of crossing more effectively.
The use of a priority queue for BreadthFirst Search (BFS) is a clever idea as well. By prioritizing the cells that are flooded earlier (i.e., have a smaller day number), you can ensure that you are always considering the earliest possible cutoff point for the land path.
However, a few things to note about this approach:
The 8directional connectivity might not always provide a performance advantage, as you could end up checking more cells than necessary. In contrast, 4directional connectivity (up, down, left, and right) would be more focused and might result in a more efficient solution.
Even with the priority queue, BFS could still take a long time if the grid is large. You might need to further optimize the search algorithm to make it more efficient.
The implementation could be more complex due to the need to handle 8directional connectivity and priority queue. You would need to carefully manage the state of the grid and the queue to ensure correctness and efficiency.
Overall, your idea provides an alternative way to approach the problem and could lead to an efficient solution, but it would need careful implementation and optimization. It could also be interesting to compare this approach with the binary search approach to see which one performs better under different scenarios.
An easy approach to solve this problem (not Fast)
Create a int[][] grid using values of cell. make a Boolean type DFS function in which we will travel from every cell in first row of grid while making sure all accessible cell of the grid have less value then current day and not revisiting any cell. in the main function run binary search between 0 and row*col. Find last maximum day in which we can travel from 0th row to last row using binary search.
Yes, you’re correct. Your solution approach would work, but it would not be the most efficient one. It combines both depthfirst search (DFS) and binary search. Here’s a rough Python code outline based on your suggestion:


In this approach, you’re initially constructing the grid where every cell represents the day it’s going to be flooded. Then you perform a binary search over the days and for each day, you perform a DFS from every cell in the first row to check if you can reach the last row.
This approach would definitely solve the problem as it covers all possibilities, but it might not be efficient for large inputs due to the nested usage of DFS inside binary search. Each DFS has a time complexity of O(row*col), and you’re performing it inside a binary search which makes it quite expensive computationally.
But this is a good starting point. To optimize this, you might want to look at other techniques, such as UnionFind or other data structures that can make the path checking process more efficient.
Dont look for the answer in the range [0, rowcol] , rather look for the answer in the range [col1, row(col1)]
Yes, your observation is correct! That’s a smart way to tighten the bounds of your binary search.
The reason is, on the first day (col1), water fills the top cell (if we’re using 0indexing), and on the last day, water fills all the cells in the last column. Since we’re interested in the last day it’s possible to traverse from top to bottom on land, it’s guaranteed that it will be within this range.
Here’s how you can adjust the binary search range in Python:


This reduces the search space and hence reduces the computational cost of your solution. However, keep in mind that the time complexity remains the same, because in terms of big O notation, we do not consider constants or smaller terms.
Your references to Shiloach, Y.; Even, S. (1981) and Holm, J.; De Lichtenberg, K.; Thorup, M. (2001) indicate that you’re interested in sophisticated algorithms for dynamic connectivity problems. These kinds of problems indeed have a deep connection (no pun intended) to the problem you’re trying to solve here.
The idea behind dynamic connectivity problems is this: we have a network of points, and over time, the connections between these points can change. We want to be able to efficiently answer questions like “is there a path between point A and point B?” even as the network changes over time.
This is similar to the problem you’re dealing with, where the “network” is the grid of land cells and the connections between them change over time as more and more cells become water.
The algorithms from the papers you’ve mentioned deal with these kind of problems. The paper by Shiloach and Even presents an algorithm for efficiently handling the case where edges are only removed (just like in our problem, where land cells only become water). The paper by Holm, De Lichtenberg, and Thorup presents more advanced techniques for handling cases where edges can be both added and removed.
While these algorithms can theoretically be applied to our problem, they may be overkill in practice. The binary search approach we’ve discussed before can solve the problem in a more straightforward way. However, if you’re interested in learning more about these kinds of advanced algorithmic techniques, I highly recommend checking out the papers you’ve mentioned, as well as taking courses or reading books on data structures and algorithms. They’re a rich and fascinating field of study!
We don’t check columns. We check individual cells in the matrix. All I had to do was adjust my cells so that they were 0based indexed and my solution ran fine.
That’s correct. Since the input cells are 1based indexed, you need to adjust the indices to make them 0based before using them in most programming languages such as Python, Java, C++, etc., as these languages use 0based indexing.
The adjustment is usually done by subtracting 1 from each of the indices. Here’s a brief demonstration of how you could adjust your cells to 0based index in Python:


In the adjusted_cells
, the cell coordinates are now 0based indexed, which you can use to index your matrix.
Remember, these conversions are necessary as indexing in Python (and many other languages) starts at 0, not 1. Without these adjustments, you could get “index out of range” errors, or your logic might not work correctly because you’re looking at the wrong cells.
Does this matter, day 0 will be all lands anyway.
Yes, you’re correct. The problem statement specifies that initially, on day 0, the entire matrix is land. Therefore, no matter what the cells input is, the matrix will be entirely land on day 0.
However, starting from day 1, the cells specified in the input begin to become water. The sequence in which these cells become water is critical to solving the problem because the goal is to determine the last day where it is possible to walk from the top to the bottom by only walking on land cells.
So, while the initial state of the matrix on day 0 is always land, the subsequent changes to the matrix as specified by the cells input are what you need to consider in order to solve the problem.
cells.length == row * col The only constraint which makes Disjoint Set solution possible… I feel it should’ve been mentioned in the question too.
You’re correct, the constraint cells.length == row * col
is very crucial in this problem because it means every cell in the matrix will eventually become water. This constraint indeed enables us to utilize the Disjoint Set/UnionFind data structure to solve the problem.
That said, in most problem statements, such constraints are mentioned in the constraints section rather than in the question itself. It is a part of the challenge to read and understand how the constraints can be used to approach the problem.
In competitive programming or algorithmic challenges, often the constraints can give insights into what data structures or algorithms can be used to solve the problem efficiently. It’s always important to carefully read and consider how the constraints might guide the solution.
I agree that it should have been in the description (I missed it too) but you don’t need it for disjoint set if you focus on the water (with diagonals) rather than the land.
Absolutely, your perspective is spot on. The problem can be tackled by considering the water cells and their diagonals, instead of focusing on the land. By using a Disjoint Set Union (DSU) data structure, we can keep track of the connectivity between water cells, considering diagonal connections as well.
Here’s a rough idea:
For each cell that becomes water, perform “union” operations in your DSU for that cell and its neighbouring (including diagonally) water cells.
After each “union” operation, check if there’s a connection between the top row and the bottom row by checking if any of the top and bottom row cells belong to the same set.
This approach flips the problem on its head, considering connectivity of water instead of land, and indeed doesn’t rely on the condition that every cell must eventually turn into water. It’s another way to tackle the problem, illustrating the diversity of approaches in algorithmic problem solving.
Remember, the implementation of this idea would also need to handle edge cases and the specific requirements of the problem.
Use the disjoint set data structure for water In every element in cells, either create a new set or joint existing set. Once any set those width is col (left boundary is 1 and right boundary is col), it is impossible to walk
Yes, you are on the right track. The problem could be solved using a disjointset data structure (also known as UnionFind) for managing the groups of cells covered with water.
Here’s an outline of how the approach would work:
Initialize a disjoint set data structure. Each cell that gets filled with water will be represented as a set in this structure.
For each day, when a new cell gets filled with water (
cells[i]
), add it to the disjoint set data structure. This could mean creating a new set or merging with an existing set, depending on whether the neighbouring cells (including diagonals) are water or not.For each newly added water cell, check its neighbours. If any of them are already water, you perform a “union” operation to merge them into the same set.
After each “union” operation, check if there’s a connection between the top row and the bottom row. This can be done by checking if any of the cells in the top row and any of the cells in the bottom row belong to the same set in your disjointset structure.
When any set spans from the first column to the last column (
col
), it means there’s a water path spanning from the left to the right side of the grid, making it impossible to walk from top to bottom. So, we can return the current day number.
Remember to adjust your indexes and handle edge cases as necessary for your implementation. This approach is efficient as the UnionFind operations can be done nearly in constant time.
can someone optimize My BFSDFS solution


Your current solution uses a DFS method in a BFS manner to check for each day if there exists a path from the top row to the bottom row. Although it works for smaller inputs, it can lead to a Time Limit Exceeded (TLE) error for larger ones. This is because for every new flooded cell, you are running a DFS from scratch which has a time complexity of O(row * col). And you’re doing this for each day, which makes the total time complexity of your solution O((row * col)^2).
The code can be optimized using a disjoint set (unionfind) data structure, and it works by reversing the problem: starting from the last day, then go back day by day and change the water cell back to land, then use unionfind to check if the top row is connected to the bottom row. If they are connected, that means we can still walk from top to bottom on this day.
Here’s a Python example of how you can do it:


This optimized code significantly improves the time complexity of the solution by eliminating the need to perform a DFS for each day. Instead, it updates the grid in reverse order (from last day to first) and only performs unionfind operations for the new land cell and its neighbors. It’s also worth noting that the “top” and “bottom” virtual nodes are added to facilitate the checking of connectivity between the top row and the bottom row.
This approach should work well for the problem’s constraints, but you might need to adjust it a bit to fit your requirements.
IF ANYONE CAN HELP WHAT IS WRONG WITH THIS CODE WOULD MEAN A LOT TO ME
class Disjoint{ public: vectorsize; vectorparent; Disjoint(int V){ size.resize(V,1); parent.resize(V); for(int i = 0;i<V;i++){ parent[i] = i; } }
int findUlt(int u){
if(parent[u] == u){
return u;
}
return parent[u] = findUlt(parent[u]);
}
void unionbysize(int u,int v){
int ultu = findUlt(u);
int ultv = findUlt(v);
if(ultu == ultv){
return;
}
if(size[ultu] < size[ultv]){
size[ultv] += size[ultu];
parent[ultu] = ultv;
}
else{
size[ultu] += size[ultv];
parent[ultv] = ultu;
}
}
};
class Solution { public: int latestDayToCross(int row, int col, vector<vector>& cells) { Disjoint ds1(row*col+2); vector<vector>mat(row,vector(col,1)); int count = 0; for(int i = cells.size()1;i>=0;i–){ count++; int r = cells[i][0]1; int c = cells[i][1]1; mat[r][c] = 0;
if(r == 0){
ds1.unionbysize(0,c+1);
}
else if(r == row1){
ds1.unionbysize(row*col+1,(col*r) + c+1);
}
vector<int>adjnodes = {0,1,0,1};
for(int delcol = 0,delrow = 3;delcol<4&& delrow>=0;delcol++,delrow){
int nr = r + adjnodes[delrow];
int nc = c + adjnodes[delcol];
if(nr>=0 && nr<row && nc>=0 && nc<col && mat[nr][nc] == 0){
int nodeidx = col*r + c+1;
int adjnodeidx = col*nr + nc+1;
ds1.unionbysize(nodeidx,adjnodeidx);
}
}
for(auto i:ds1.parent){
cout<<i<<" ";
}
cout<<endl;
if(ds1.parent[0] == ds1.parent[row*col+1]){
return row*colcount;
}
}
return 1;
}
};
Looking at your code, the issue lies in the logic to check the connectivity between the top row and the bottom row.
Instead of comparing ds1.parent[0]
and ds1.parent[row*col+1]
, you should find their ultimate parent (root of the disjoint set) and compare those. Replace the line with:


The reason is that in a disjoint set, two elements belong to the same set if and only if their ultimate parents (roots) are the same.
Also, you are missing some unions. When the r == 0
or r == row1
, you also need to check the 4 adjacent cells, and union them if they are not water. Add the following code after the if (r == 0)
and else if (r == row1)
blocks:


Please note that it’s also possible that there could be more issues in your code, but these are the main ones I have found. I suggest testing your code with different cases after applying these changes. Let me know if you have any other questions.
`class Solution { public: bool dfs(int r, int c, vector& mat, vector& vis, int n, int m, int dr[], int dc[]){ vis[r][c]=1; if(r==n1)return true;
for(int k=0;k<4;k++){
int nr = r + dr[k];
int nc = c + dc[k];
if(nr>=0 and nr<n and nc>=0 and nc<m and mat[nr][nc]!=1 and vis[nr][nc]!=1){
if(dfs(nr,nc,mat,vis,n,m,dr,dc))return true;
}
}
return false;
} bool check(int &mid, vector<vector>& cells, vector<vector>& mat, int &row, int &col, int dr[], int dc[]){ for(int i=0;i<mid;i++){ mat[cells[i][0]1][cells[i][1]1]=1; }
for(int i=0;i<col; i++){
vector<vector<int>>vis(row, vector<int> (col, 0));
if(mat[0][i] == 0){
if(dfs(0,i,mat,vis,row,col,dr, dc))return true;
}
}
return false;
}
int latestDayToCross(int row, int col, vector<vector>& cells) {
int t = 0, b = cells.size()1;
int ans = 0;
int dr[] = {1,0,1,0};
int dc[] = {0,1,0,1};
while(t<=b){
int mid = (t+b)>>1;
vector<vector<int>> mat(row, vector<int> (col, 0));
if(check(mid, cells, mat, row, col, dr, dc)){
ans = mid;
t = mid+1;
}
else b = mid1;
}
return ans;
} };`
this code giving error on last test case and when i am using that test case normally it is giving right ans……leetcode saying using of static or global variables are affecting it… any soution
The issue you are facing could be related to memory reuse across different test cases. In other words, the static or global variables that you’ve used in your solution are not getting reset after each test case.
In this specific code you provided, there is no usage of static or global variables. The code seems correct and should work perfectly fine.
However, if you have static or global variables in your original code that is not shown in this provided code snippet, ensure that you reinitialize or clear those variables at the beginning of the latestDayToCross
function.
To be more specific, in terms of the ‘static’ keyword, in C++, static variables inside a function keep their values between function calls. So, if you are using a static variable, it may retain an old value which might cause an issue in subsequent test cases.
In terms of global variables, they also maintain their value across multiple function calls and could similarly cause an issue if not properly reinitialized or cleared.
If you still face the issue, please provide the full code with the static or global variables so I could provide a more accurate response.
hash table for cell:day, reverse bfs for possibility and binary search for day.
Based on your description, here is a Python3 solution using a hash table to map cell to day, then reverse BFS to check if it’s possible to cross on that day, and finally, Binary Search to find the latest day to cross.


This solution starts by mapping each cell to the day it becomes water. This is done in the for loop where i
represents the day and (x, y)
represents the cell. Next, we use Binary Search to search for the latest day where it’s possible to cross from top to bottom. This is achieved by calling the bfs
function which performs a BFS from the last row and tries to reach the first row. The bfs
function only visits cells that are water on or after the day m
and if it’s able to reach the first row, it returns True. If not, it returns False. The Binary Search uses this information to decide whether to look for a later day (if bfs
returned True) or an earlier day (if bfs
returned False). The Binary Search ends when l == r
and l
is returned as the result.
The time constraints on this are too loose for it to be a hard. The hint says to use binary search, which is probably why they allow it. Checking if there is a valid path is O(n) (where n = row * col). So doing a binary search is O(n log n). There are at least two ways you can do it in O(n) time  especially when you notice the constraint that means that the entire land eventually floods. These constraints would make it worthy of a hard label 2 <= row, col <= 2 * 10^4 cells.length < 10^6 1 <= ri <= row 1 <= ci <= col
You’re correct, this problem has an optimal solution with O(n) complexity. The key observation is that all cells will be flooded eventually, so we can handle the procedure in reverse order, i.e., “unflood” the cells one by one.
A way to solve this in O(n) time complexity is by using the Disjoint Set Union (DSU) data structure. The main idea is to keep track of all components (or connected cells) and their borders. If at some moment we have two adjacent components and one has the top border and the other has the bottom border, it means we can walk from top to bottom.
Here’s a Python solution to illustrate this:


In this solution, we first create a DSU instance for each cell. For each day, starting from the last, we unflood the corresponding cell and try to merge it with its nonflooded neighbors. If after merging the component of the cell spans from top to bottom, we update the result with the current day and break the loop. The index
function is used to convert 2D cell coordinates to 1D array indices for the DSU. In the DSU class, `find
,
unionand
connectedmethods are standard DSU operations.
top_bottom` checks whether the component of a cell spans from top to bottom.
This solution has an O(n) time complexity because we only make one pass through the cells
list and perform DSU operations which are nearly constant time.
This question is misleading. I spend an hour thinking that I can only move one cell in one day, instead, this question is only looking for a valid path!
You’re correct that the objective is to find a valid path across the flooded cells, rather than moving one cell per day.
For problems like these, it’s important to carefully read and understand the problem statement before diving into the solution. This is especially true for more complex problems where the optimal strategy often involves utilizing some nonobvious property of the problem, as is the case here with considering the problem in reverse.
In summary, the problem is asking you to find the latest day by which it’s still possible to cross from the top to the bottom of the grid by only walking on land cells. So, the goal is indeed to find a valid path, but under the constraint that you want this path to exist as late as possible.
One tip that might be helpful is that during the BFS, if you reach the last row then you can terminate the BFS immediately. I don’t know if you will get a TLE if you don’t exit early but it is something to consider if you are getting TLEs.
You are absolutely right! Implementing early termination of a search algorithm like BFS when a successful path is found can considerably optimize your code, reducing unnecessary computations.
However, do note that such optimization heavily depends on the structure of the problem. In cases where we want the shortest path, we cannot stop at the first path we found (unless we use an algorithm like Dijkstra). In this problem, where we only need to validate the existence of a path, early termination is indeed beneficial.
For anyone reading this, here’s how you’d generally implement early termination in a BFS:


In this code, if the BFS reaches the end_node (or in our original problem, the last row), it returns True immediately, without the need to visit the remaining nodes in the queue. This can save significant time when the graph is large.
No need for binary search here, a Disjoint Set works just fine. Make sure to use ranking when merging cells.
Yes, that’s right! A Disjoint Set Union (DSU) data structure can be very useful for this problem. This is because it’s great for handling problems that involve merging of groups or determining whether two elements belong to the same group (in this case, checking if a cell is reachable from the top row and if we can get to the bottom row from it).
The main idea is to start by treating each cell as a separate set. Then, for each day, when a cell is flooded, it can be merged with the sets of its neighbouring cells that have already been flooded. If at any point the top row and bottom row belong to the same set, we know a path has been formed.
Here’s an outline of how you could implement it:


With this approach, you can skip the binary search part entirely. Also, DSU operations are usually more efficient than search operations, as the time complexity for union and find operations is nearly O(1) when path compression and union by rank are used.
Just remember to update the DSU in reverse order of the days (starting from the last day), because we’re essentially trying to find the last day when the top and bottom are not connected.
Q .Why this DFS gives Runtime Error?Am i missing some case or the thought process itself is wrong.I don’t wanna know about optimisation just what’s the problem with this. Please Help!!! `class Solution { public: int dfs(int i,int j,int day,vector&grid,vector& cells){ if(i==grid.size()1){ return day; } int days=INT_MIN; vectormoves{{1,0},{1,0},{0,1},{0,1}}; for(int k=0;k<4;++k){ int newRow=i+moves[k][0]; int newCol=j+moves[k][1];
if(newRow<0  newCol<0  newRow==grid.size()  newCol==grid[0].size()  grid[newRow][newCol]==1)
continue;
if(day<cells.size() && newRow==(cells[day][0]1) && newCol==(cells[day][1]1)){
continue;
}
grid[cells[day][0]][cells[day][1]]=1;
int val=dfs(newRow,newCol,day+1,grid,cells);
days=max(val,days);
grid[cells[day][0]][cells[day][1]]=0;
}
return days;
}
int latestDayToCross(int row, int col, vector<vector>& cells) {
int ans=INT_MIN;
vector<vector>grid(row,vector(col,0));
for(int j=0;j<col;++j){
int val=dfs(0,j,0,grid,cells);
ans=max(ans,val);
}
return ans;
}
};`
The runtime error may be due to stack overflow, since you’re using recursion (DFS) in a potentially large 2D grid. This problem has up to 210^4 rows and columns, which means there could be up to 410^8 possible cells. This is a vast number and could result in the maximum recursion depth being exceeded, hence leading to a stack overflow error.
Here are a few potential issues and fixes:
DFS Limit: By default, the depth of recursion in C++ can be limited depending on the system. The stack size for a C++ program can be limited (for instance, 1MB in some environments). You can try to increase the stack limit, but this might not be a scalable solution given the constraints of this problem.
DFS Optimization: As it stands, the DFS approach is doing redundant work. You might be visiting the same cell more than once and computing the same subproblem multiple times, which is inefficient. Consider memoization (storing previously computed results) to avoid recomputation of the same subproblem.
Check bounds correctly: Your bounds checking line
if(newRow<0  newCol<0  newRow==grid.size()  newCol==grid[0].size()  grid[newRow][newCol]==1)
should actually beif(newRow<0  newCol<0  newRow>=grid.size()  newCol>=grid[0].size()  grid[newRow][newCol]==1)
. You should use >= instead of == for comparing newRow and newCol with the grid dimensions.
But to be honest, for this problem, using DFS may not be the best approach. It’s prone to stack overflow errors due to deep recursion, and it’s not particularly efficient in this case. Other strategies, such as Binary Search, BFS or Disjoint Set Union, would be more suitable and scalable for this problem.
Try to analyze the problem constraints more deeply before deciding on the best algorithm to use. Recursion (DFS) can be suitable for smaller problems, but it might not be scalable for larger problems with high constraints.
How to identify that this problem is an inverse problem?
In this problem, we start with a fully flooded grid and then drain the water cell by cell (each cell represents a day). The task is to find out the day on which it is no longer possible to cross the grid from top to bottom. This is the inverse of starting with an empty grid and flooding it day by day.
Typically, problems are categorized as inverse when the forward process is more difficult to model or understand, but the reversed process is easier. In this problem, it is more challenging to model the path from top to bottom as water fills the grid because the number of paths decreases as more cells become flooded. However, if we reverse the problem and start with a flooded grid, we can easily track the path’s existence as we drain the water. The task then becomes finding the last possible day (cell) we can drain while still maintaining at least one valid path.
To identify an inverse problem, you need to look at the problem and see if reversing the process makes it simpler or more logical to solve. For any problem, the key is to try and simplify it as much as possible to make it easier to solve, and sometimes looking at it from a reverse perspective can help.
Is it the same as solving a problem backwards?
Yes, in a sense, solving a problem backwards or in reverse often refers to the same process as an inverse problem. An inverse problem requires you to determine the cause or inputs based on the observable effects or outputs. In this context, you’re essentially “rewinding” the problem and trying to find the initial conditions based on the final state.
However, it’s important to note that not all problems can be effectively solved this way, and sometimes trying to solve a problem in reverse might be just as complex, if not more so, than solving it in the original direction. But in the cases where the reverse process is easier to understand or more efficiently modelled, like in this grid problem, the concept of inverse problemsolving can be extremely useful.
So, while it might be the same as “solving a problem backwards” in casual terms, the formal term “inverse problem” has more specific connotations in fields like mathematics, physics, and computer science, where it often refers to a particular type of problemsolving strategy where you infer the inputs from the outputs.
Alternative approach: use union find (disjoint set union)
The idea is to link neighboring cells – and when they cover all space between left and right border , we reach the stopping condition. The complexity is O(row * col)
Yes, you’re correct. A UnionFind (or Disjoint Set Union) data structure is indeed an excellent alternative to solve this problem. The UnionFind data structure provides an efficient way to keep track of a partitioning of a set into disjoint subsets.
Here is the basic idea of using a UnionFind in this problem:
 Initialize a UnionFind data structure with one node representing each cell in the grid.
 Start from the last day and go backwards to the first day. For each day, add the cell that becomes land on that day to the grid.
 After adding a cell, perform the union operation on the cell and its neighboring cells that are also lands.
 After each union operation, check if there is a path from the top row to the bottom row. In terms of the UnionFind data structure, check if there is any cell in the top row that is in the same set as any cell in the bottom row.
 The first day that there is a path from the top row to the bottom row is the answer. Since we are going from the last day to the first day, we stop at the first day that we find such a path and return it as the answer.
Remember, in this approach, it’s critical to go through the days in reverse order, i.e., from the last day to the first day. This is because we are essentially finding the last day that we can cross the grid, which is easier to find by going backwards from when all cells are lands.
The time complexity of this approach is approximately O(row * col * alpha), where alpha is the inverse Ackermann function. Since alpha is a very slowgrowing function, it can be considered almost constant for all practical purposes, and hence, the time complexity can be considered as nearly O(row * col), as you’ve mentioned.
HOW DO I OPTIMISE THIS CODE . IT’S GIVING TLE FOR 104/118 TESTCASE.
class Solution { public:
void bfs(int & ans , vector& track , unordered_map<int,int>& mp , int c , int r ) { queue<pair<int,int» q ;
for(int i = 0; i< c ; i++) { q.push({i,mp[i]1}); track[i] = mp[i]1 ; }
while(!q.empty()) {
int a[] = {c,1,1,c};
int sz = q.size();
for(int i = 0 ; i< sz ; i++)
{
auto fr = q.front();
q.pop();
if(fr.first < r*c and fr.first >= c*(r1))
{
ans = max(ans , fr.second );
}
for(int t = 0 ; t< 4 ;t ++)
{
int nind = fr.first + a[t];
if( !( (t == 1  t == 2) and (nind/c != fr.first/c) ) and nind>= 0 and nind < c*r and (track[nind] == 1  track[nind] < min(fr.second , mp[nind]1) ) )
{
q.push({ nind , min(fr.second , mp[nind]1) });
track[nind] = min(fr.second , mp[nind]1) ;
}
}
}
} }
int latestDayToCross(int row, int col, vector<vector>& cells) { unordered_map<int,int> mp ; int t = 1 ; for(auto it : cells) { mp[(it[0]1)*col + (it[1]1)] = t ; t++; }
int ans = 0 ; vector track (row*col+1,1); bfs(ans,track,mp,col,row);
return ans ; } };
This code seems to be applying BreadthFirst Search (BFS) to solve the problem. However, the time complexity of BFS is linear to the number of vertices and edges in the graph, so when the number of cells (vertices) is large, this could lead to a TLE (Time Limit Exceeded) error.
The problem here lies in that you’re pushing cells into the queue without checking if they have already been visited or not, causing repeated calculations. As the size of the grid grows, the number of repeated calculations increases dramatically, leading to TLE.
To optimize this code, there are several things you can do:
Avoid Duplicate Calculations: You are pushing each cell into the queue multiple times if it can be reached from multiple other cells. This can lead to a lot of duplicate calculations. To avoid this, you can add a visited array to keep track of cells that you have already processed. Only if a cell has not been visited before, push it into the queue and mark it as visited.
Use binary search: Since the number of days is sorted, you can use binary search to drastically reduce the number of BFS you need to perform. The idea is to find the maximum number of days such that you can still cross the river. Start from the middle number of days, if you can cross, move to the upper half, else move to the lower half. This reduces the time complexity from O(n) to O(logn), where n is the number of days.
Use a faster data structure: If you’re still getting TLE after applying the above optimizations, you may need to use a faster data structure. For this problem, UnionFind could be a better fit. The basic idea is the same as BFS, but UnionFind can perform the union and find operations in nearly constant time, which could be much faster than BFS when the number of cells is large.
Please keep in mind that these are general optimization techniques and may not directly apply to your code. You may need to adjust your code accordingly to fit these optimizations.