Trains the technique from
LeetCode 1970Last Day Where You Can Still CrossThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A tidal flat is laid out as a grid of rows rows and cols columns. Rows are numbered 1 through rows starting at the north jetty, so row 1 runs along the north jetty and row rows runs along the south jetty. Columns are numbered 1 through cols from west to east.
Every square starts dry. Days are numbered from 1, and on day d the square floods[d - 1] = [r, c] goes under water and stays under from then on. The list covers every square of the flat exactly once, so by day rows * cols nothing is dry.
A walk across on a given day starts on any dry square in row 1, ends on any dry square in row rows, and moves from a square to a dry square sharing a side with it, meaning one step north, south, east or west. Diagonal steps are not allowed, and every square on the walk must be dry that day.
Return the largest day number on which a walk across is still possible.
Example 1
On day 2 the flooded squares are (2,2) and (1,1). The route (1,3), (2,3), (3,3) starts in row 1 on the north jetty, steps south twice onto dry squares, and finishes in row 3 on the south jetty. On day 3 the square (3,3) also floods.
Example 2
After the first three squares flood, the dry squares are (1,3), (2,1) and (2,3), and the step from (1,3) to (2,3) crosses from the north jetty row to the south jetty row.
Example 3
The first twelve days flood columns 1, 2 and 3 completely and leave column 4 untouched, so on day 12 the four squares of column 4 form a walk from the north jetty row to the south jetty row. Column 4 starts flooding on day 13.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def last_dry_day(rows: int, cols: int, floods: list[list[int]]) -> int:public int lastDryDay(int rows, int cols, int[][] floods)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.