-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1970 (#3549)
No.1970.Last Day Where You Can Still Cross
- Loading branch information
Showing
12 changed files
with
1,509 additions
and
362 deletions.
There are no files selected for viewing
627 changes: 513 additions & 114 deletions
627
solution/1900-1999/1970.Last Day Where You Can Still Cross/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
627 changes: 513 additions & 114 deletions
627
solution/1900-1999/1970.Last Day Where You Can Still Cross/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
70 changes: 39 additions & 31 deletions
70
solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
class Solution { | ||
public: | ||
vector<int> p; | ||
int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; | ||
int row, col; | ||
|
||
int latestDayToCross(int row, int col, vector<vector<int>>& 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<vector<bool>> grid(row, vector<bool>(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<pair<int, int>> 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<vector<bool>>& 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; | ||
} | ||
}; | ||
}; |
75 changes: 37 additions & 38 deletions
75
solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |
82 changes: 42 additions & 40 deletions
82
solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<int[]> 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; | ||
} | ||
} | ||
} |
49 changes: 24 additions & 25 deletions
49
solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
41 changes: 41 additions & 0 deletions
41
solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Oops, something went wrong.