Search⌘ K
AI Features

Last Day Where You Can Still Cross

Understand how to apply the union-find algorithm to determine the last day you can cross a binary matrix from top to bottom only through land cells. This lesson helps you analyze connectivity in a grid where cells gradually flood, enhancing your problem-solving skills for graph and union-find patterns in coding interviews.

Statement

You are given two integers, rows and cols, which represent the number of rows and columns in a 1-based binary matrixA 1-based matrix has an indexing that starts from 1 instead of 0, and a binary matrix contains only binary values, i.e., 0 and 1. Therefore, a 1-based binary matrix is a matrix containing only 0s and 1s, whose indexes (for both rows and columns) start from 1.. In this matrix, each 00 represents land, and each 11 represents water.

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 00 to 11. 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]=[ri,ci]waterCells[i] = [r_{i},c_{i}] ...