diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/README.md b/solution/1900-1999/1970.Last Day Where You Can Still Cross/README.md index d32f41c311c71..a0057f8a94ff9 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/README.md +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/README.md @@ -76,7 +76,13 @@ tags: -### 方法一 +### 方法一:二分查找 + BFS + +我们注意到,如果我们能在第 $k$ 天从最上面一行走到最下面一行,那么对于任意 $0 \lt k' \lt k$,我们也能在第 $k'$ 天从最上面一行走到最下面一行。这存在着单调性,因此,我们可以使用二分查找,找到最大的 $k$,使得我们能在第 $k$ 天从最上面一行走到最下面一行。 + +我们定义二分查找的左边界 $l = 1$,右边界 $r = |cells|$,其中 $|cells|$ 表示数组 $cells$ 的长度。然后,我们二分枚举 $k$,对于每一个 $k$,我们取 $\textit{cells}$ 的前 $k$ 个元素,将这些元素对应的格子变成水域,然后使用广度优先搜索,从最上面一行开始,尝试走到最下面一行。如果我们能走到最下面一行,那么说明我们可以在第 $k$ 天从最上面一行走到最下面一行,我们就将左边界 $l$ 更新为 $k$,否则,我们将右边界 $r$ 更新为 $k - 1$。 + +时间复杂度 $O(m \times n \times \log (m \times n))$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数。 @@ -85,84 +91,365 @@ tags: ```python class Solution: def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: + def check(k: int) -> bool: + g = [[0] * col for _ in range(row)] + for i, j in cells[:k]: + g[i - 1][j - 1] = 1 + q = [(0, j) for j in range(col) if g[0][j] == 0] + for x, y in q: + if x == row - 1: + return True + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + q.append((nx, ny)) + g[nx][ny] = 1 + return False + n = row * col - p = list(range(n + 2)) - grid = [[False] * col for _ in range(row)] - top, bottom = n, n + 1 - - def find(x): - if p[x] != x: - p[x] = find(p[x]) - return p[x] - - def check(i, j): - return 0 <= i < row and 0 <= j < col and grid[i][j] - - for k in range(len(cells) - 1, -1, -1): - i, j = cells[k][0] - 1, cells[k][1] - 1 - grid[i][j] = True - for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: - if check(i + x, j + y): - p[find(i * col + j)] = find((i + x) * col + j + y) - if i == 0: - p[find(i * col + j)] = find(top) - if i == row - 1: - p[find(i * col + j)] = find(bottom) - if find(top) == find(bottom): - return k - return 0 + l, r = 1, n + dirs = (-1, 0, 1, 0, -1) + while l < r: + mid = (l + r + 1) >> 1 + if check(mid): + l = mid + else: + r = mid - 1 + return l ``` #### Java ```java class Solution { - private int[] p; - private int row; - private int col; - private boolean[][] grid; - private int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; + private int[][] cells; + private int m; + private int n; public int latestDayToCross(int row, int col, int[][] cells) { - int n = row * col; - this.row = row; - this.col = col; - p = new int[n + 2]; - for (int i = 0; i < p.length; ++i) { - p[i] = i; + int l = 1, r = cells.length; + this.cells = cells; + this.m = row; + this.n = col; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } + + private boolean check(int k) { + int[][] g = new int[m][n]; + for (int i = 0; i < k; i++) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; + } + final int[] dirs = {-1, 0, 1, 0, -1}; + Deque q = new ArrayDeque<>(); + for (int j = 0; j < n; j++) { + if (g[0][j] == 0) { + q.offer(new int[] {0, j}); + g[0][j] = 1; + } } - grid = new boolean[row][col]; - int top = n, bottom = n + 1; - for (int k = cells.length - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (int[] e : dirs) { - if (check(i + e[0], j + e[1])) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); + while (!q.isEmpty()) { + int[] p = q.poll(); + int x = p[0], y = p[1]; + if (x == m - 1) { + return true; + } + for (int i = 0; i < 4; i++) { + int nx = x + dirs[i], ny = y + dirs[i + 1]; + if (nx >= 0 && nx < m && ny >= 0 && ny < n && g[nx][ny] == 0) { + q.offer(new int[] {nx, ny}); + g[nx][ny] = 1; } } - if (i == 0) { - p[find(i * col + j)] = find(top); + } + return false; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int latestDayToCross(int row, int col, vector>& cells) { + int l = 1, r = cells.size(); + int g[row][col]; + int dirs[5] = {0, 1, 0, -1, 0}; + auto check = [&](int k) -> bool { + memset(g, 0, sizeof(g)); + for (int i = 0; i < k; ++i) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; + } + queue> q; + for (int j = 0; j < col; ++j) { + if (g[0][j] == 0) { + q.emplace(0, j); + g[0][j] = 1; + } } - if (i == row - 1) { - p[find(i * col + j)] = find(bottom); + while (!q.empty()) { + auto [x, y] = q.front(); + q.pop(); + if (x == row - 1) { + return true; + } + for (int i = 0; i < 4; ++i) { + int nx = x + dirs[i]; + int ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + q.emplace(nx, ny); + g[nx][ny] = 1; + } + } } - if (find(top) == find(bottom)) { - return k; + return false; + }; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; } } - return 0; + return l; } +}; +``` - private int find(int x) { +#### Go + +```go +func latestDayToCross(row int, col int, cells [][]int) int { + l, r := 1, len(cells) + dirs := [5]int{-1, 0, 1, 0, -1} + check := func(k int) bool { + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) + } + for i := 0; i < k; i++ { + g[cells[i][0]-1][cells[i][1]-1] = 1 + } + q := [][2]int{} + for j := 0; j < col; j++ { + if g[0][j] == 0 { + g[0][j] = 1 + q = append(q, [2]int{0, j}) + } + } + for len(q) > 0 { + x, y := q[0][0], q[0][1] + q = q[1:] + if x == row-1 { + return true + } + for i := 0; i < 4; i++ { + nx, ny := x+dirs[i], y+dirs[i+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + g[nx][ny] = 1 + q = append(q, [2]int{nx, ny}) + } + } + } + return false + } + for l < r { + mid := (l + r + 1) >> 1 + if check(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l +} +``` + +#### TypeScript + +```ts +function latestDayToCross(row: number, col: number, cells: number[][]): number { + let [l, r] = [1, cells.length]; + const check = (k: number): boolean => { + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(0)); + for (let i = 0; i < k; ++i) { + const [x, y] = cells[i]; + g[x - 1][y - 1] = 1; + } + const q: number[][] = []; + for (let j = 0; j < col; ++j) { + if (g[0][j] === 0) { + q.push([0, j]); + g[0][j] = 1; + } + } + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (const [x, y] of q) { + if (x === row - 1) { + return true; + } + for (let i = 0; i < 4; ++i) { + const nx = x + dirs[i]; + const ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + q.push([nx, ny]); + g[nx][ny] = 1; + } + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +``` + + + + + + + +### 方法二:并查集 + +我们可以先将所有的陆地格子初始化为 $1$,然后倒序遍历数组 $\textit{cells}$,将每个格子对应的陆地格子变成 $0$,并将其与上下左右的陆地格子合并。我们还需要维护两个虚拟节点 $s$ 和 $t$,分别表示最上面一行和最下面一行的虚拟节点。如果 $s$ 和 $t$ 在并查集中连通,那么说明我们可以在第 $i$ 天从最上面一行走到最下面一行。 + +时间复杂度 $O(m \times n \times \alpha(m \times n))$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数,而 $\alpha$ 表示 Ackermann 函数的反函数。 + + + +#### Python3 + +```python +class UnionFind: + def __init__(self, n): + self.p = list(range(n)) + self.size = [1] * n + + def find(self, x): + if self.p[x] != x: + self.p[x] = self.find(self.p[x]) + return self.p[x] + + def union(self, a, b): + pa, pb = self.find(a), self.find(b) + if pa == pb: + return False + if self.size[pa] > self.size[pb]: + self.p[pb] = pa + self.size[pa] += self.size[pb] + else: + self.p[pa] = pb + self.size[pb] += self.size[pa] + return True + + +class Solution: + def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: + mn = len(cells) + uf = UnionFind(mn + 2) + s, t = mn, mn + 1 + dirs = (-1, 0, 1, 0, -1) + g = [[1] * col for _ in range(row)] + for i in range(mn - 1, -1, -1): + x, y = cells[i][0] - 1, cells[i][1] - 1 + g[x][y] = 0 + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + uf.union(x * col + y, nx * col + ny) + if x == 0: + uf.union(y, s) + if x == row - 1: + uf.union(x * col + y, t) + if uf.find(s) == uf.find(t): + return i +``` + +#### Java + +```java +class UnionFind { + private final int[] p; + private final int[] size; + + public UnionFind(int n) { + p = new int[n]; + size = new int[n]; + for (int i = 0; i < n; ++i) { + p[i] = i; + size[i] = 1; + } + } + + public int find(int x) { if (p[x] != x) { p[x] = find(p[x]); } return p[x]; } - private boolean check(int i, int j) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; + public boolean union(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; + } +} + +class Solution { + public int latestDayToCross(int row, int col, int[][] cells) { + int mn = cells.length; + UnionFind uf = new UnionFind(mn + 2); + int s = mn, t = mn + 1; + int[][] g = new int[row][col]; + for (var e : g) { + Arrays.fill(e, 1); + } + final int[] dirs = {-1, 0, 1, 0, -1}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.union(s, x * col + y); + } + if (x == row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } } } ``` @@ -170,93 +457,205 @@ class Solution { #### C++ ```cpp -class Solution { +class UnionFind { public: - vector p; - int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; - int row, col; - - int latestDayToCross(int row, int col, vector>& cells) { - int n = row * col; - this->row = row; - this->col = col; - p.resize(n + 2); - for (int i = 0; i < p.size(); ++i) p[i] = i; - vector> grid(row, vector(col, false)); - int top = n, bottom = n + 1; - for (int k = cells.size() - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (auto e : dirs) { - if (check(i + e[0], j + e[1], grid)) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); - } - } - if (i == 0) p[find(i * col + j)] = find(top); - if (i == row - 1) p[find(i * col + j)] = find(bottom); - if (find(top) == find(bottom)) return k; - } - return 0; + UnionFind(int n) { + p = vector(n); + size = vector(n, 1); + iota(p.begin(), p.end(), 0); } - bool check(int i, int j, vector>& grid) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; + bool unite(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; } int find(int x) { - if (p[x] != x) p[x] = find(p[x]); + if (p[x] != x) { + p[x] = find(p[x]); + } return p[x]; } + +private: + vector p, size; +}; + +class Solution { +public: + int latestDayToCross(int row, int col, vector>& cells) { + int mn = cells.size(); + UnionFind uf(mn + 2); + int s = mn, t = mn + 1; + vector> g(row, vector(col, 1)); + const int dirs[5] = {0, 1, 0, -1, 0}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.unite(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.unite(s, x * col + y); + } + if (x == row - 1) { + uf.unite(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } + } }; ``` #### Go ```go -var p []int +type unionFind struct { + p, size []int +} -func latestDayToCross(row int, col int, cells [][]int) int { - n := row * col - p = make([]int, n+2) - for i := 0; i < len(p); i++ { +func newUnionFind(n int) *unionFind { + p := make([]int, n) + size := make([]int, n) + for i := range p { p[i] = i + size[i] = 1 } - grid := make([][]bool, row) - for i := 0; i < row; i++ { - grid[i] = make([]bool, col) + return &unionFind{p, size} +} + +func (uf *unionFind) find(x int) int { + if uf.p[x] != x { + uf.p[x] = uf.find(uf.p[x]) + } + return uf.p[x] +} + +func (uf *unionFind) union(a, b int) bool { + pa, pb := uf.find(a), uf.find(b) + if pa == pb { + return false + } + if uf.size[pa] > uf.size[pb] { + uf.p[pb] = pa + uf.size[pa] += uf.size[pb] + } else { + uf.p[pa] = pb + uf.size[pb] += uf.size[pa] + } + return true +} + +func latestDayToCross(row int, col int, cells [][]int) int { + mn := len(cells) + uf := newUnionFind(mn + 2) + s, t := mn, mn+1 + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) + for j := range g[i] { + g[i][j] = 1 + } } - top, bottom := n, n+1 - dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}} - for k := len(cells) - 1; k >= 0; k-- { - i, j := cells[k][0]-1, cells[k][1]-1 - grid[i][j] = true - for _, e := range dirs { - if check(i+e[0], j+e[1], grid) { - p[find(i*col+j)] = find((i+e[0])*col + j + e[1]) + dirs := [5]int{-1, 0, 1, 0, -1} + for i := mn - 1; ; i-- { + x, y := cells[i][0]-1, cells[i][1]-1 + g[x][y] = 0 + for j := 0; j < 4; j++ { + nx, ny := x+dirs[j], y+dirs[j+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + uf.union(x*col+y, nx*col+ny) } } - if i == 0 { - p[find(i*col+j)] = find(top) + if x == 0 { + uf.union(s, x*col+y) } - if i == row-1 { - p[find(i*col+j)] = find(bottom) + if x == row-1 { + uf.union(t, x*col+y) } - if find(top) == find(bottom) { - return k + if uf.find(s) == uf.find(t) { + return i } } - return 0 } +``` + +#### TypeScript + +```ts +class UnionFind { + p: number[]; + size: number[]; + constructor(n: number) { + this.p = Array(n) + .fill(0) + .map((_, i) => i); + this.size = Array(n).fill(1); + } -func check(i, j int, grid [][]bool) bool { - return i >= 0 && i < len(grid) && j >= 0 && j < len(grid[0]) && grid[i][j] + find(x: number): number { + if (this.p[x] !== x) { + this.p[x] = this.find(this.p[x]); + } + return this.p[x]; + } + + union(a: number, b: number): boolean { + const [pa, pb] = [this.find(a), this.find(b)]; + if (pa === pb) { + return false; + } + if (this.size[pa] > this.size[pb]) { + this.p[pb] = pa; + this.size[pa] += this.size[pb]; + } else { + this.p[pa] = pb; + this.size[pb] += this.size[pa]; + } + return true; + } } -func find(x int) int { - if p[x] != x { - p[x] = find(p[x]) - } - return p[x] +function latestDayToCross(row: number, col: number, cells: number[][]): number { + const mn = cells.length; + const uf = new UnionFind(row * col + 2); + const [s, t] = [mn, mn + 1]; + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(1)); + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (let i = mn - 1; ; --i) { + const [x, y] = [cells[i][0] - 1, cells[i][1] - 1]; + g[x][y] = 0; + for (let j = 0; j < 4; ++j) { + const [nx, ny] = [x + dirs[j], y + dirs[j + 1]]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x === 0) { + uf.union(s, y); + } + if (x === row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) === uf.find(t)) { + return i; + } + } } ``` diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/README_EN.md b/solution/1900-1999/1970.Last Day Where You Can Still Cross/README_EN.md index 2351c186e3228..9b58aecbf49b6 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/README_EN.md +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/README_EN.md @@ -77,7 +77,13 @@ The last day where it is possible to cross from top to bottom is on day 3. -### Solution 1 +### Solution 1: Binary Search + BFS + +We note that if we can walk from the top row to the bottom row on day $k$, then for any $0 < k' < k$, we can also walk from the top row to the bottom row on day $k'$. This exhibits monotonicity, so we can use binary search to find the largest $k$ such that we can walk from the top row to the bottom row on day $k$. + +We define the left boundary of the binary search as $l = 1$ and the right boundary as $r = |cells|$, where $|cells|$ represents the length of the array $\textit{cells}$. Then, we perform binary search on $k$. For each $k$, we take the first $k$ elements of $\textit{cells}$, turn the corresponding cells into water, and then use breadth-first search (BFS) to try to walk from the top row to the bottom row. If we can reach the bottom row, it means we can walk from the top row to the bottom row on day $k$, so we update the left boundary $l$ to $k$. Otherwise, we update the right boundary $r$ to $k - 1$. + +The time complexity is $O(m \times n \times \log (m \times n))$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ represent the number of rows and columns of the matrix, respectively. @@ -86,84 +92,365 @@ The last day where it is possible to cross from top to bottom is on day 3. ```python class Solution: def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: + def check(k: int) -> bool: + g = [[0] * col for _ in range(row)] + for i, j in cells[:k]: + g[i - 1][j - 1] = 1 + q = [(0, j) for j in range(col) if g[0][j] == 0] + for x, y in q: + if x == row - 1: + return True + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + q.append((nx, ny)) + g[nx][ny] = 1 + return False + n = row * col - p = list(range(n + 2)) - grid = [[False] * col for _ in range(row)] - top, bottom = n, n + 1 - - def find(x): - if p[x] != x: - p[x] = find(p[x]) - return p[x] - - def check(i, j): - return 0 <= i < row and 0 <= j < col and grid[i][j] - - for k in range(len(cells) - 1, -1, -1): - i, j = cells[k][0] - 1, cells[k][1] - 1 - grid[i][j] = True - for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: - if check(i + x, j + y): - p[find(i * col + j)] = find((i + x) * col + j + y) - if i == 0: - p[find(i * col + j)] = find(top) - if i == row - 1: - p[find(i * col + j)] = find(bottom) - if find(top) == find(bottom): - return k - return 0 + l, r = 1, n + dirs = (-1, 0, 1, 0, -1) + while l < r: + mid = (l + r + 1) >> 1 + if check(mid): + l = mid + else: + r = mid - 1 + return l ``` #### Java ```java class Solution { - private int[] p; - private int row; - private int col; - private boolean[][] grid; - private int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; + private int[][] cells; + private int m; + private int n; public int latestDayToCross(int row, int col, int[][] cells) { - int n = row * col; - this.row = row; - this.col = col; - p = new int[n + 2]; - for (int i = 0; i < p.length; ++i) { - p[i] = i; + int l = 1, r = cells.length; + this.cells = cells; + this.m = row; + this.n = col; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; + } + + private boolean check(int k) { + int[][] g = new int[m][n]; + for (int i = 0; i < k; i++) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; + } + final int[] dirs = {-1, 0, 1, 0, -1}; + Deque q = new ArrayDeque<>(); + for (int j = 0; j < n; j++) { + if (g[0][j] == 0) { + q.offer(new int[] {0, j}); + g[0][j] = 1; + } } - grid = new boolean[row][col]; - int top = n, bottom = n + 1; - for (int k = cells.length - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (int[] e : dirs) { - if (check(i + e[0], j + e[1])) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); + while (!q.isEmpty()) { + int[] p = q.poll(); + int x = p[0], y = p[1]; + if (x == m - 1) { + return true; + } + for (int i = 0; i < 4; i++) { + int nx = x + dirs[i], ny = y + dirs[i + 1]; + if (nx >= 0 && nx < m && ny >= 0 && ny < n && g[nx][ny] == 0) { + q.offer(new int[] {nx, ny}); + g[nx][ny] = 1; } } - if (i == 0) { - p[find(i * col + j)] = find(top); + } + return false; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int latestDayToCross(int row, int col, vector>& cells) { + int l = 1, r = cells.size(); + int g[row][col]; + int dirs[5] = {0, 1, 0, -1, 0}; + auto check = [&](int k) -> bool { + memset(g, 0, sizeof(g)); + for (int i = 0; i < k; ++i) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; + } + queue> q; + for (int j = 0; j < col; ++j) { + if (g[0][j] == 0) { + q.emplace(0, j); + g[0][j] = 1; + } } - if (i == row - 1) { - p[find(i * col + j)] = find(bottom); + while (!q.empty()) { + auto [x, y] = q.front(); + q.pop(); + if (x == row - 1) { + return true; + } + for (int i = 0; i < 4; ++i) { + int nx = x + dirs[i]; + int ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + q.emplace(nx, ny); + g[nx][ny] = 1; + } + } } - if (find(top) == find(bottom)) { - return k; + return false; + }; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; } } - return 0; + return l; } +}; +``` - private int find(int x) { +#### Go + +```go +func latestDayToCross(row int, col int, cells [][]int) int { + l, r := 1, len(cells) + dirs := [5]int{-1, 0, 1, 0, -1} + check := func(k int) bool { + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) + } + for i := 0; i < k; i++ { + g[cells[i][0]-1][cells[i][1]-1] = 1 + } + q := [][2]int{} + for j := 0; j < col; j++ { + if g[0][j] == 0 { + g[0][j] = 1 + q = append(q, [2]int{0, j}) + } + } + for len(q) > 0 { + x, y := q[0][0], q[0][1] + q = q[1:] + if x == row-1 { + return true + } + for i := 0; i < 4; i++ { + nx, ny := x+dirs[i], y+dirs[i+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + g[nx][ny] = 1 + q = append(q, [2]int{nx, ny}) + } + } + } + return false + } + for l < r { + mid := (l + r + 1) >> 1 + if check(mid) { + l = mid + } else { + r = mid - 1 + } + } + return l +} +``` + +#### TypeScript + +```ts +function latestDayToCross(row: number, col: number, cells: number[][]): number { + let [l, r] = [1, cells.length]; + const check = (k: number): boolean => { + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(0)); + for (let i = 0; i < k; ++i) { + const [x, y] = cells[i]; + g[x - 1][y - 1] = 1; + } + const q: number[][] = []; + for (let j = 0; j < col; ++j) { + if (g[0][j] === 0) { + q.push([0, j]); + g[0][j] = 1; + } + } + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (const [x, y] of q) { + if (x === row - 1) { + return true; + } + for (let i = 0; i < 4; ++i) { + const nx = x + dirs[i]; + const ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + q.push([nx, ny]); + g[nx][ny] = 1; + } + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} +``` + + + + + + + +### Solution 2: Union-Find + +We can first initialize all land cells as $1$, then traverse the array $\textit{cells}$ in reverse order, turning each corresponding land cell into $0$ and merging it with the adjacent land cells (up, down, left, right). We also need to maintain two virtual nodes $s$ and $t$, representing the virtual nodes for the top row and the bottom row, respectively. If $s$ and $t$ are connected in the union-find set, it means we can walk from the top row to the bottom row on day $i$. + +The time complexity is $O(m \times n \times \alpha(m \times n))$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ represent the number of rows and columns of the matrix, respectively, and $\alpha$ represents the inverse Ackermann function. + + + +#### Python3 + +```python +class UnionFind: + def __init__(self, n): + self.p = list(range(n)) + self.size = [1] * n + + def find(self, x): + if self.p[x] != x: + self.p[x] = self.find(self.p[x]) + return self.p[x] + + def union(self, a, b): + pa, pb = self.find(a), self.find(b) + if pa == pb: + return False + if self.size[pa] > self.size[pb]: + self.p[pb] = pa + self.size[pa] += self.size[pb] + else: + self.p[pa] = pb + self.size[pb] += self.size[pa] + return True + + +class Solution: + def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: + mn = len(cells) + uf = UnionFind(mn + 2) + s, t = mn, mn + 1 + dirs = (-1, 0, 1, 0, -1) + g = [[1] * col for _ in range(row)] + for i in range(mn - 1, -1, -1): + x, y = cells[i][0] - 1, cells[i][1] - 1 + g[x][y] = 0 + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + uf.union(x * col + y, nx * col + ny) + if x == 0: + uf.union(y, s) + if x == row - 1: + uf.union(x * col + y, t) + if uf.find(s) == uf.find(t): + return i +``` + +#### Java + +```java +class UnionFind { + private final int[] p; + private final int[] size; + + public UnionFind(int n) { + p = new int[n]; + size = new int[n]; + for (int i = 0; i < n; ++i) { + p[i] = i; + size[i] = 1; + } + } + + public int find(int x) { if (p[x] != x) { p[x] = find(p[x]); } return p[x]; } - private boolean check(int i, int j) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; + public boolean union(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; + } +} + +class Solution { + public int latestDayToCross(int row, int col, int[][] cells) { + int mn = cells.length; + UnionFind uf = new UnionFind(mn + 2); + int s = mn, t = mn + 1; + int[][] g = new int[row][col]; + for (var e : g) { + Arrays.fill(e, 1); + } + final int[] dirs = {-1, 0, 1, 0, -1}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.union(s, x * col + y); + } + if (x == row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } } } ``` @@ -171,93 +458,205 @@ class Solution { #### C++ ```cpp -class Solution { +class UnionFind { public: - vector p; - int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; - int row, col; - - int latestDayToCross(int row, int col, vector>& cells) { - int n = row * col; - this->row = row; - this->col = col; - p.resize(n + 2); - for (int i = 0; i < p.size(); ++i) p[i] = i; - vector> grid(row, vector(col, false)); - int top = n, bottom = n + 1; - for (int k = cells.size() - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (auto e : dirs) { - if (check(i + e[0], j + e[1], grid)) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); - } - } - if (i == 0) p[find(i * col + j)] = find(top); - if (i == row - 1) p[find(i * col + j)] = find(bottom); - if (find(top) == find(bottom)) return k; - } - return 0; + UnionFind(int n) { + p = vector(n); + size = vector(n, 1); + iota(p.begin(), p.end(), 0); } - bool check(int i, int j, vector>& grid) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; + bool unite(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; } int find(int x) { - if (p[x] != x) p[x] = find(p[x]); + if (p[x] != x) { + p[x] = find(p[x]); + } return p[x]; } + +private: + vector p, size; +}; + +class Solution { +public: + int latestDayToCross(int row, int col, vector>& cells) { + int mn = cells.size(); + UnionFind uf(mn + 2); + int s = mn, t = mn + 1; + vector> g(row, vector(col, 1)); + const int dirs[5] = {0, 1, 0, -1, 0}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.unite(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.unite(s, x * col + y); + } + if (x == row - 1) { + uf.unite(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } + } }; ``` #### Go ```go -var p []int +type unionFind struct { + p, size []int +} -func latestDayToCross(row int, col int, cells [][]int) int { - n := row * col - p = make([]int, n+2) - for i := 0; i < len(p); i++ { +func newUnionFind(n int) *unionFind { + p := make([]int, n) + size := make([]int, n) + for i := range p { p[i] = i + size[i] = 1 } - grid := make([][]bool, row) - for i := 0; i < row; i++ { - grid[i] = make([]bool, col) + return &unionFind{p, size} +} + +func (uf *unionFind) find(x int) int { + if uf.p[x] != x { + uf.p[x] = uf.find(uf.p[x]) + } + return uf.p[x] +} + +func (uf *unionFind) union(a, b int) bool { + pa, pb := uf.find(a), uf.find(b) + if pa == pb { + return false + } + if uf.size[pa] > uf.size[pb] { + uf.p[pb] = pa + uf.size[pa] += uf.size[pb] + } else { + uf.p[pa] = pb + uf.size[pb] += uf.size[pa] + } + return true +} + +func latestDayToCross(row int, col int, cells [][]int) int { + mn := len(cells) + uf := newUnionFind(mn + 2) + s, t := mn, mn+1 + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) + for j := range g[i] { + g[i][j] = 1 + } } - top, bottom := n, n+1 - dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}} - for k := len(cells) - 1; k >= 0; k-- { - i, j := cells[k][0]-1, cells[k][1]-1 - grid[i][j] = true - for _, e := range dirs { - if check(i+e[0], j+e[1], grid) { - p[find(i*col+j)] = find((i+e[0])*col + j + e[1]) + dirs := [5]int{-1, 0, 1, 0, -1} + for i := mn - 1; ; i-- { + x, y := cells[i][0]-1, cells[i][1]-1 + g[x][y] = 0 + for j := 0; j < 4; j++ { + nx, ny := x+dirs[j], y+dirs[j+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + uf.union(x*col+y, nx*col+ny) } } - if i == 0 { - p[find(i*col+j)] = find(top) + if x == 0 { + uf.union(s, x*col+y) } - if i == row-1 { - p[find(i*col+j)] = find(bottom) + if x == row-1 { + uf.union(t, x*col+y) } - if find(top) == find(bottom) { - return k + if uf.find(s) == uf.find(t) { + return i } } - return 0 } +``` + +#### TypeScript + +```ts +class UnionFind { + p: number[]; + size: number[]; + constructor(n: number) { + this.p = Array(n) + .fill(0) + .map((_, i) => i); + this.size = Array(n).fill(1); + } -func check(i, j int, grid [][]bool) bool { - return i >= 0 && i < len(grid) && j >= 0 && j < len(grid[0]) && grid[i][j] + find(x: number): number { + if (this.p[x] !== x) { + this.p[x] = this.find(this.p[x]); + } + return this.p[x]; + } + + union(a: number, b: number): boolean { + const [pa, pb] = [this.find(a), this.find(b)]; + if (pa === pb) { + return false; + } + if (this.size[pa] > this.size[pb]) { + this.p[pb] = pa; + this.size[pa] += this.size[pb]; + } else { + this.p[pa] = pb; + this.size[pb] += this.size[pa]; + } + return true; + } } -func find(x int) int { - if p[x] != x { - p[x] = find(p[x]) - } - return p[x] +function latestDayToCross(row: number, col: number, cells: number[][]): number { + const mn = cells.length; + const uf = new UnionFind(row * col + 2); + const [s, t] = [mn, mn + 1]; + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(1)); + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (let i = mn - 1; ; --i) { + const [x, y] = [cells[i][0] - 1, cells[i][1] - 1]; + g[x][y] = 0; + for (let j = 0; j < 4; ++j) { + const [nx, ny] = [x + dirs[j], y + dirs[j + 1]]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x === 0) { + uf.union(s, y); + } + if (x === row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) === uf.find(t)) { + return i; + } + } } ``` diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp index 0d26dfbf279f3..45ab993807efe 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp @@ -1,38 +1,46 @@ class Solution { public: - vector p; - int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; - int row, col; - int latestDayToCross(int row, int col, vector>& cells) { - int n = row * col; - this->row = row; - this->col = col; - p.resize(n + 2); - for (int i = 0; i < p.size(); ++i) p[i] = i; - vector> grid(row, vector(col, false)); - int top = n, bottom = n + 1; - for (int k = cells.size() - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (auto e : dirs) { - if (check(i + e[0], j + e[1], grid)) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); + int l = 1, r = cells.size(); + int g[row][col]; + int dirs[5] = {0, 1, 0, -1, 0}; + auto check = [&](int k) -> bool { + memset(g, 0, sizeof(g)); + for (int i = 0; i < k; ++i) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; + } + queue> q; + for (int j = 0; j < col; ++j) { + if (g[0][j] == 0) { + q.emplace(0, j); + g[0][j] = 1; + } + } + while (!q.empty()) { + auto [x, y] = q.front(); + q.pop(); + if (x == row - 1) { + return true; + } + for (int i = 0; i < 4; ++i) { + int nx = x + dirs[i]; + int ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + q.emplace(nx, ny); + g[nx][ny] = 1; + } } } - if (i == 0) p[find(i * col + j)] = find(top); - if (i == row - 1) p[find(i * col + j)] = find(bottom); - if (find(top) == find(bottom)) return k; + return false; + }; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } } - return 0; - } - - bool check(int i, int j, vector>& grid) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; - } - - int find(int x) { - if (p[x] != x) p[x] = find(p[x]); - return p[x]; + return l; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.go b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.go index ef87100b1a850..dfc4f5d60c677 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.go +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.go @@ -1,45 +1,44 @@ -var p []int - func latestDayToCross(row int, col int, cells [][]int) int { - n := row * col - p = make([]int, n+2) - for i := 0; i < len(p); i++ { - p[i] = i - } - grid := make([][]bool, row) - for i := 0; i < row; i++ { - grid[i] = make([]bool, col) - } - top, bottom := n, n+1 - dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}} - for k := len(cells) - 1; k >= 0; k-- { - i, j := cells[k][0]-1, cells[k][1]-1 - grid[i][j] = true - for _, e := range dirs { - if check(i+e[0], j+e[1], grid) { - p[find(i*col+j)] = find((i+e[0])*col + j + e[1]) - } + l, r := 1, len(cells) + dirs := [5]int{-1, 0, 1, 0, -1} + check := func(k int) bool { + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) } - if i == 0 { - p[find(i*col+j)] = find(top) + for i := 0; i < k; i++ { + g[cells[i][0]-1][cells[i][1]-1] = 1 + } + q := [][2]int{} + for j := 0; j < col; j++ { + if g[0][j] == 0 { + g[0][j] = 1 + q = append(q, [2]int{0, j}) + } } - if i == row-1 { - p[find(i*col+j)] = find(bottom) + for len(q) > 0 { + x, y := q[0][0], q[0][1] + q = q[1:] + if x == row-1 { + return true + } + for i := 0; i < 4; i++ { + nx, ny := x+dirs[i], y+dirs[i+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + g[nx][ny] = 1 + q = append(q, [2]int{nx, ny}) + } + } } - if find(top) == find(bottom) { - return k + return false + } + for l < r { + mid := (l + r + 1) >> 1 + if check(mid) { + l = mid + } else { + r = mid - 1 } } - return 0 + return l } - -func check(i, j int, grid [][]bool) bool { - return i >= 0 && i < len(grid) && j >= 0 && j < len(grid[0]) && grid[i][j] -} - -func find(x int) int { - if p[x] != x { - p[x] = find(p[x]) - } - return p[x] -} \ No newline at end of file diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.java b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.java index 5fa5ee5d0fb22..f0f2b9f485cbb 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.java +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.java @@ -1,49 +1,51 @@ class Solution { - private int[] p; - private int row; - private int col; - private boolean[][] grid; - private int[][] dirs = new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; + private int[][] cells; + private int m; + private int n; public int latestDayToCross(int row, int col, int[][] cells) { - int n = row * col; - this.row = row; - this.col = col; - p = new int[n + 2]; - for (int i = 0; i < p.length; ++i) { - p[i] = i; - } - grid = new boolean[row][col]; - int top = n, bottom = n + 1; - for (int k = cells.length - 1; k >= 0; --k) { - int i = cells[k][0] - 1, j = cells[k][1] - 1; - grid[i][j] = true; - for (int[] e : dirs) { - if (check(i + e[0], j + e[1])) { - p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); - } - } - if (i == 0) { - p[find(i * col + j)] = find(top); - } - if (i == row - 1) { - p[find(i * col + j)] = find(bottom); - } - if (find(top) == find(bottom)) { - return k; + int l = 1, r = cells.length; + this.cells = cells; + this.m = row; + this.n = col; + while (l < r) { + int mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; } } - return 0; + return l; } - private int find(int x) { - if (p[x] != x) { - p[x] = find(p[x]); + private boolean check(int k) { + int[][] g = new int[m][n]; + for (int i = 0; i < k; i++) { + g[cells[i][0] - 1][cells[i][1] - 1] = 1; } - return p[x]; - } - - private boolean check(int i, int j) { - return i >= 0 && i < row && j >= 0 && j < col && grid[i][j]; + final int[] dirs = {-1, 0, 1, 0, -1}; + Deque q = new ArrayDeque<>(); + for (int j = 0; j < n; j++) { + if (g[0][j] == 0) { + q.offer(new int[] {0, j}); + g[0][j] = 1; + } + } + while (!q.isEmpty()) { + int[] p = q.poll(); + int x = p[0], y = p[1]; + if (x == m - 1) { + return true; + } + for (int i = 0; i < 4; i++) { + int nx = x + dirs[i], ny = y + dirs[i + 1]; + if (nx >= 0 && nx < m && ny >= 0 && ny < n && g[nx][ny] == 0) { + q.offer(new int[] {nx, ny}); + g[nx][ny] = 1; + } + } + } + return false; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.py b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.py index 4dba4b6162de8..e962ff5472330 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.py +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.py @@ -1,28 +1,27 @@ class Solution: def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: - n = row * col - p = list(range(n + 2)) - grid = [[False] * col for _ in range(row)] - top, bottom = n, n + 1 - - def find(x): - if p[x] != x: - p[x] = find(p[x]) - return p[x] + def check(k: int) -> bool: + g = [[0] * col for _ in range(row)] + for i, j in cells[:k]: + g[i - 1][j - 1] = 1 + q = [(0, j) for j in range(col) if g[0][j] == 0] + for x, y in q: + if x == row - 1: + return True + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + q.append((nx, ny)) + g[nx][ny] = 1 + return False - def check(i, j): - return 0 <= i < row and 0 <= j < col and grid[i][j] - - for k in range(len(cells) - 1, -1, -1): - i, j = cells[k][0] - 1, cells[k][1] - 1 - grid[i][j] = True - for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]: - if check(i + x, j + y): - p[find(i * col + j)] = find((i + x) * col + j + y) - if i == 0: - p[find(i * col + j)] = find(top) - if i == row - 1: - p[find(i * col + j)] = find(bottom) - if find(top) == find(bottom): - return k - return 0 + n = row * col + l, r = 1, n + dirs = (-1, 0, 1, 0, -1) + while l < r: + mid = (l + r + 1) >> 1 + if check(mid): + l = mid + else: + r = mid - 1 + return l diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.ts b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.ts new file mode 100644 index 0000000000000..e17b0c9c3e54e --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.ts @@ -0,0 +1,41 @@ +function latestDayToCross(row: number, col: number, cells: number[][]): number { + let [l, r] = [1, cells.length]; + const check = (k: number): boolean => { + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(0)); + for (let i = 0; i < k; ++i) { + const [x, y] = cells[i]; + g[x - 1][y - 1] = 1; + } + const q: number[][] = []; + for (let j = 0; j < col; ++j) { + if (g[0][j] === 0) { + q.push([0, j]); + g[0][j] = 1; + } + } + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (const [x, y] of q) { + if (x === row - 1) { + return true; + } + for (let i = 0; i < 4; ++i) { + const nx = x + dirs[i]; + const ny = y + dirs[i + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + q.push([nx, ny]); + g[nx][ny] = 1; + } + } + } + return false; + }; + while (l < r) { + const mid = (l + r + 1) >> 1; + if (check(mid)) { + l = mid; + } else { + r = mid - 1; + } + } + return l; +} diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.cpp b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.cpp new file mode 100644 index 0000000000000..09ee9e3de0cb8 --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.cpp @@ -0,0 +1,63 @@ +class UnionFind { +public: + UnionFind(int n) { + p = vector(n); + size = vector(n, 1); + iota(p.begin(), p.end(), 0); + } + + bool unite(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; + } + + int find(int x) { + if (p[x] != x) { + p[x] = find(p[x]); + } + return p[x]; + } + +private: + vector p, size; +}; + +class Solution { +public: + int latestDayToCross(int row, int col, vector>& cells) { + int mn = cells.size(); + UnionFind uf(mn + 2); + int s = mn, t = mn + 1; + vector> g(row, vector(col, 1)); + const int dirs[5] = {0, 1, 0, -1, 0}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.unite(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.unite(s, x * col + y); + } + if (x == row - 1) { + uf.unite(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } + } +}; diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.go b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.go new file mode 100644 index 0000000000000..7ce73b53b9f93 --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.go @@ -0,0 +1,68 @@ +type unionFind struct { + p, size []int +} + +func newUnionFind(n int) *unionFind { + p := make([]int, n) + size := make([]int, n) + for i := range p { + p[i] = i + size[i] = 1 + } + return &unionFind{p, size} +} + +func (uf *unionFind) find(x int) int { + if uf.p[x] != x { + uf.p[x] = uf.find(uf.p[x]) + } + return uf.p[x] +} + +func (uf *unionFind) union(a, b int) bool { + pa, pb := uf.find(a), uf.find(b) + if pa == pb { + return false + } + if uf.size[pa] > uf.size[pb] { + uf.p[pb] = pa + uf.size[pa] += uf.size[pb] + } else { + uf.p[pa] = pb + uf.size[pb] += uf.size[pa] + } + return true +} + +func latestDayToCross(row int, col int, cells [][]int) int { + mn := len(cells) + uf := newUnionFind(mn + 2) + s, t := mn, mn+1 + g := make([][]int, row) + for i := range g { + g[i] = make([]int, col) + for j := range g[i] { + g[i][j] = 1 + } + } + dirs := [5]int{-1, 0, 1, 0, -1} + for i := mn - 1; ; i-- { + x, y := cells[i][0]-1, cells[i][1]-1 + g[x][y] = 0 + for j := 0; j < 4; j++ { + nx, ny := x+dirs[j], y+dirs[j+1] + if nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0 { + uf.union(x*col+y, nx*col+ny) + } + } + if x == 0 { + uf.union(s, x*col+y) + } + if x == row-1 { + uf.union(t, x*col+y) + } + if uf.find(s) == uf.find(t) { + return i + } + } +} diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.java b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.java new file mode 100644 index 0000000000000..03c5594ccbad3 --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.java @@ -0,0 +1,67 @@ +class UnionFind { + private final int[] p; + private final int[] size; + + public UnionFind(int n) { + p = new int[n]; + size = new int[n]; + for (int i = 0; i < n; ++i) { + p[i] = i; + size[i] = 1; + } + } + + public int find(int x) { + if (p[x] != x) { + p[x] = find(p[x]); + } + return p[x]; + } + + public boolean union(int a, int b) { + int pa = find(a), pb = find(b); + if (pa == pb) { + return false; + } + if (size[pa] > size[pb]) { + p[pb] = pa; + size[pa] += size[pb]; + } else { + p[pa] = pb; + size[pb] += size[pa]; + } + return true; + } +} + +class Solution { + public int latestDayToCross(int row, int col, int[][] cells) { + int mn = cells.length; + UnionFind uf = new UnionFind(mn + 2); + int s = mn, t = mn + 1; + int[][] g = new int[row][col]; + for (var e : g) { + Arrays.fill(e, 1); + } + final int[] dirs = {-1, 0, 1, 0, -1}; + for (int i = mn - 1;; --i) { + int x = cells[i][0] - 1, y = cells[i][1] - 1; + g[x][y] = 0; + for (int j = 0; j < 4; ++j) { + int nx = x + dirs[j], ny = y + dirs[j + 1]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] == 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x == 0) { + uf.union(s, x * col + y); + } + if (x == row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) == uf.find(t)) { + return i; + } + } + } +} diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.py b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.py new file mode 100644 index 0000000000000..9254260bf5f87 --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.py @@ -0,0 +1,43 @@ +class UnionFind: + def __init__(self, n): + self.p = list(range(n)) + self.size = [1] * n + + def find(self, x): + if self.p[x] != x: + self.p[x] = self.find(self.p[x]) + return self.p[x] + + def union(self, a, b): + pa, pb = self.find(a), self.find(b) + if pa == pb: + return False + if self.size[pa] > self.size[pb]: + self.p[pb] = pa + self.size[pa] += self.size[pb] + else: + self.p[pa] = pb + self.size[pb] += self.size[pa] + return True + + +class Solution: + def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int: + mn = len(cells) + uf = UnionFind(mn + 2) + s, t = mn, mn + 1 + dirs = (-1, 0, 1, 0, -1) + g = [[1] * col for _ in range(row)] + for i in range(mn - 1, -1, -1): + x, y = cells[i][0] - 1, cells[i][1] - 1 + g[x][y] = 0 + for a, b in pairwise(dirs): + nx, ny = x + a, y + b + if 0 <= nx < row and 0 <= ny < col and g[nx][ny] == 0: + uf.union(x * col + y, nx * col + ny) + if x == 0: + uf.union(y, s) + if x == row - 1: + uf.union(x * col + y, t) + if uf.find(s) == uf.find(t): + return i diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.ts b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.ts new file mode 100644 index 0000000000000..5c1e48711bbb4 --- /dev/null +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution2.ts @@ -0,0 +1,59 @@ +class UnionFind { + p: number[]; + size: number[]; + constructor(n: number) { + this.p = Array(n) + .fill(0) + .map((_, i) => i); + this.size = Array(n).fill(1); + } + + find(x: number): number { + if (this.p[x] !== x) { + this.p[x] = this.find(this.p[x]); + } + return this.p[x]; + } + + union(a: number, b: number): boolean { + const [pa, pb] = [this.find(a), this.find(b)]; + if (pa === pb) { + return false; + } + if (this.size[pa] > this.size[pb]) { + this.p[pb] = pa; + this.size[pa] += this.size[pb]; + } else { + this.p[pa] = pb; + this.size[pb] += this.size[pa]; + } + return true; + } +} + +function latestDayToCross(row: number, col: number, cells: number[][]): number { + const mn = cells.length; + const uf = new UnionFind(row * col + 2); + const [s, t] = [mn, mn + 1]; + const g: number[][] = Array.from({ length: row }, () => Array(col).fill(1)); + const dirs: number[] = [-1, 0, 1, 0, -1]; + for (let i = mn - 1; ; --i) { + const [x, y] = [cells[i][0] - 1, cells[i][1] - 1]; + g[x][y] = 0; + for (let j = 0; j < 4; ++j) { + const [nx, ny] = [x + dirs[j], y + dirs[j + 1]]; + if (nx >= 0 && nx < row && ny >= 0 && ny < col && g[nx][ny] === 0) { + uf.union(x * col + y, nx * col + ny); + } + } + if (x === 0) { + uf.union(s, y); + } + if (x === row - 1) { + uf.union(t, x * col + y); + } + if (uf.find(s) === uf.find(t)) { + return i; + } + } +}