# Solution: Last Day Where You Can Still Cross

Let's solve the Last Day Where You Can Still Cross problem using the Union Find pattern.

## We'll cover the following

## Statement

You are given two integers, `rows`

and `cols`

, which represent the number of rows and columns in a

Initially, on day 0, the whole matrix will just be all 0s, that is, all land. With each passing day, one of the cells of this matrix will get flooded and, therefore, will change to water, that is, from $0$ to $1$. This continues until the entire matrix is flooded. You are given a 1-based array, `waterCells`

, that records which cell will be flooded on each day. Each element $waterCells[i] = [r_{i},c_{i}]$ indicates the cell present at the ${r_{i}}^{th}$ row and ${c_{i}}^{th}$ column of the matrix that will change from land to water on the $i^{th}$ day.

We can cross any cell of the matrix as long as itâ€™s land. Once it changes to water, we canâ€™t cross it. To cross any cell, we can only move in one of the four `waterCells`

, you are required to find the last day where you can still cross the matrix, from top to bottom, by walking over the land cells only.

Note:You can start from any cell in the top row, and you need to be able to reach just one cell in the bottom row for it to count as a crossing.

**Constraints:**

- $2 \leq$
`rows`

$,$`cols`

$\leq 50$ - $4 \leq$
`rows`

$\times$`cols`

$\leq 2500$ `waterCells.length`

$==$`rows`

$\times$`cols`

- $1 \leq r_{i} \leq$
`rows`

- $1 \leq c_{i} \leq$
`cols`

- All values of
`waterCells`

are unique.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.