LeetCode Challenge Day 107 β 1970. Last Day Where You Can Still Cross
Nitin Ahirwal / December 31, 2025
Hey folks π
This is Day 107 of my LeetCode streak π
Today's problem is 1970. Last Day Where You Can Still Cross β a deceptively tricky grid problem that rewards recognizing monotonic behavior.
π Problem Statement
You are given:
- Two integers
rowandcol - A list
cells, wherecells[i] = [r, c]represents a cell that becomes flooded on dayi
Rules:
- On day 0, the entire grid is land
- Each day, exactly one land cell turns into water
- You can move up, down, left, right
- You may start from any cell in the top row
- You must reach any cell in the bottom row
Goal:
Return the last day on which it is still possible to walk from the top to the bottom using only land cells.
π‘ Intuition
As days progress, land cells keep turning into water β and never revert back.
This gives us a crucial observation:
If crossing is possible on day
d, it must be possible on all days< d.
If crossing is impossible on dayd, it will remain impossible for all days> d.
This monotonic property immediately suggests using binary search on the answer.
The only remaining question is:
How do we efficiently check if crossing is possible on a given day?
Thatβs where BFS on a grid comes in.
π Approach
- Binary search the day range
[1, row Γ col] - For a given day
mid:- Build the grid
- Flood the first
midcells
- Run BFS:
- Start from all land cells in the top row
- Traverse only through land cells
- If any path reaches the bottom row, crossing is possible
- If crossing is possible:
- Save
midas a valid answer - Try a later day
- Save
- Otherwise:
- Search earlier days
- The maximum valid day found is the final answer
β±οΈ Complexity Analysis
-
Time Complexity:
O((row Γ col) log(row Γ col))
Binary search over days, with BFS traversal for each check. -
Space Complexity:
O(row Γ col)
Grid representation, visited array, and BFS queue.
π§βπ» Code (JavaScript)
/**
* @param {number} row
* @param {number} col
* @param {number[][]} cells
* @return {number}
*/
var latestDayToCross = function(row, col, cells) {
const dirs = [[1,0], [-1,0], [0,1], [0,-1]];
function canCross(day) {
const grid = Array.from({ length: row }, () => Array(col).fill(0));
for (let i = 0; i < day; i++) {
const [r, c] = cells[i];
grid[r - 1][c - 1] = 1;
}
const queue = [];
const visited = Array.from({ length: row }, () => Array(col).fill(false));
for (let j = 0; j < col; j++) {
if (grid[0][j] === 0) {
queue.push([0, j]);
visited[0][j] = true;
}
}
while (queue.length) {
const [r, c] = queue.shift();
if (r === row - 1) return true;
for (const [dr, dc] of dirs) {
const nr = r + dr, nc = c + dc;
if (
nr >= 0 && nr < row &&
nc >= 0 && nc < col &&
!visited[nr][nc] &&
grid[nr][nc] === 0
) {
visited[nr][nc] = true;
queue.push([nr, nc]);
}
}
}
return false;
}
let left = 1, right = row * col, ans = 0;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (canCross(mid)) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
};
π― Reflection
This problem highlights a powerful pattern:
-
Monotonic feasibility β Binary Search
-
Reachability β BFS / DFS
-
Hard problems often become simple once the right pattern is spotted
A clean mix of algorithmic thinking and implementation discipline.
That wraps up Day 107 of my LeetCode challenge π₯
On to Day 108 β one problem at a time π
Happy Coding π¨βπ»