Problem
There is a 1-based binary matrix where 0
represents land and 1
represents water. You are given integers row
and col
representing the number of rows and columns in the matrix, respectively.
Initially on day 0
, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-based 2D array cells
, where cells[i] = [ri, ci]
represents that on the ith
day, the cell on the rith
row and cith
column (1-based coordinates) will be covered with water (i.e., changed to 1
).
You want to find the last day that it is possible to walk from the top to the bottom by only walking on land cells. You can start from any cell in the top row and end at any cell in the bottom row. You can only travel in the** four** cardinal directions (left, right, up, and down).
Return **the *last* day where it is possible to walk from the top to the bottom by only walking on land cells**.
Example 1:
Input: row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
Output: 2
Explanation: The above image depicts how the matrix changes each day starting from day 0.
The last day where it is possible to cross from top to bottom is on day 2.
Example 2:
Input: row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]
Output: 1
Explanation: The above image depicts how the matrix changes each day starting from day 0.
The last day where it is possible to cross from top to bottom is on day 1.
Example 3:
Input: row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]
Output: 3
Explanation: The above image depicts how the matrix changes each day starting from day 0.
The last day where it is possible to cross from top to bottom is on day 3.
Constraints:
2 <= row, col <= 2 * 104
4 <= row * col <= 2 * 104
cells.length == row * col
1 <= ri <= row
1 <= ci <= col
All the values of
cells
are unique.
Solution
/**
* @param {number} row
* @param {number} col
* @param {number[][]} cells
* @return {number}
*/
var latestDayToCross = function(row, col, cells) {
var left = 0;
var right = cells.length - 1;
while (left < right) {
var mid = left + Math.ceil((right - left) / 2);
if (canCross(row, col, cells, mid)) {
left = mid;
} else {
right = mid - 1;
}
}
return left + 1;
};
var canCross = function(row, col, cells, day) {
var grid = Array(row).fill(0).map(() => Array(col).fill(0));
for (var i = 0; i <= day; i++) {
grid[cells[i][0] - 1][cells[i][1] - 1] = 1;
}
var queue = [];
for (var i = 0; i < col; i++) {
if (grid[0][i] === 0) {
queue.push([0, i]);
grid[0][i] = 2;
}
}
while (queue.length !== 0) {
var [r, c] = queue.shift();
if (r === row - 1) return true;
if (grid[r - 1] && grid[r - 1][c] === 0) {
queue.push([r - 1, c]);
grid[r - 1][c] = 2;
}
if (grid[r + 1] && grid[r + 1][c] === 0) {
queue.push([r + 1, c]);
grid[r + 1][c] = 2;
}
if (grid[r][c - 1] === 0) {
queue.push([r, c - 1]);
grid[r][c - 1] = 2;
}
if (grid[r][c + 1] === 0) {
queue.push([r, c + 1]);
grid[r][c + 1] = 2;
}
}
return false;
};
Explain:
- check if nth day can walk through all the land from top to bottom, use BFS (Breadth first search), for each land, check top/bottom/left/right four direction can cross or not.
- if nth day can cross, the last day should be in
n ~ last
, otherwise0 ~ n - 1
, so that we can use binary search to find the answer.
Complexity:
- Time complexity : O(row * col * log(row * col)).
- Space complexity : O(row * col).