Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

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.

Related Topics:
Array, Binary Search, Depth-First Search, Breadth-First Search, Union Find, Matrix

Similar Questions:

Solution 1. Union Find

Consider A in reverse order -- find the first day (from the last day) that start connects with end.

Initially, use Union Find to connect all lands in the first row with start = M * N, and connect all lands in the last row with end = M * N + 1. For each cell (x, y), its Union Find ID is x * N + y.

Traverse A in reverse order, connect this new land (A[i][0], A[i][1]) with neighboring lands.

Once we find start connects with end, this day is the answer.

// OJ: https://leetcode.com/problems/last-day-where-you-can-still-cross
// Author: github.com/lzl124631x
// Time: O(MNlog(MN))
// Space: O(MN)
class UnionFind {
    vector<int> id;
public:
    UnionFind(int n) : id(n) {
        iota(begin(id), end(id), 0);
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    bool connected(int a, int b) {
        return find(a) == find(b);
    }
    void connect(int a, int b) {
        id[find(a)] = find(b);
    }
};
class Solution {
public:
    int latestDayToCross(int M, int N, vector<vector<int>>& A) {
        UnionFind uf(M * N + 2);
        int start = M * N, end = M * N + 1, dirs[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
        vector<vector<int>> G(M, vector<int>(N));
        for (auto &c : A) G[c[0] - 1][c[1] - 1] = 1;
        for (int j = 0; j < N; ++j) {
            if (G[0][j] == 0) uf.connect(j, start);
            if (G[M - 1][j] == 0) uf.connect((M - 1) * N + j, end);
        }
        for (int i = A.size() - 1; i >= 0; --i) {
            int x = A[i][0] - 1, y = A[i][1] - 1;
            G[x][y] = 0;
            if (x == 0) uf.connect(y, start);
            if (x == M - 1) uf.connect(x * N + y, end);
            for (auto &[dx, dy] : dirs) {
                int a = x + dx, b = y + dy;
                if (a < 0 || a >= M || b < 0 || b >= N || G[a][b] == 1) continue;
                uf.connect(x * N + y, a * N + b);
            }
            if (uf.connected(start, end)) return i;
        }
        return 0;
    }
};