Skip to content

Commit 304f7ea

Browse files
author
robot
committed
feat: $1970
1 parent 17d77f3 commit 304f7ea

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
501501
- [1871. 跳跃游戏 VII](./problems/1871.jump-game-vii.md) 👍
502502
- [1872. 石子游戏 VIII](./problems/1872.stone-game-viii.md)
503503
- [1883. 准时抵达会议现场的最小跳过休息次数](./problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md)
504+
- [1970. 你能穿过矩阵的最后一天](./problems/1970.last-day-where-you-can-still-cross.md)
504505

505506
## :trident:  anki 卡片
506507

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,6 @@
331331
- [1871. 跳跃游戏 VII](./problems/1871.jump-game-vii.md) 👍
332332
- [1872. 石子游戏 VIII](./problems/1872.stone-game-viii.md)
333333
- [1883. 准时抵达会议现场的最小跳过休息次数](./problems/5775.minimum-skips-to-arrive-at-meeting-on-time.md)
334+
- [1970. 你能穿过矩阵的最后一天](./problems/1970.last-day-where-you-can-still-cross.md)
334335

335336
- [后序](epilogue.md)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
## 题目地址(1970. 你能穿过矩阵的最后一天)
2+
3+
https://leetcode-cn.com/problems/last-day-where-you-can-still-cross/
4+
5+
## 题目描述
6+
7+
```
8+
给你一个下标从 1 开始的二进制矩阵,其中 0 表示陆地,1 表示水域。同时给你 row 和 col 分别表示矩阵中行和列的数目。
9+
10+
一开始在第 0 天,整个 矩阵都是 陆地 。但每一天都会有一块新陆地被 水 淹没变成水域。给你一个下标从 1 开始的二维数组 cells ,其中 cells[i] = [ri, ci] 表示在第 i 天,第 ri 行 ci 列(下标都是从 1 开始)的陆地会变成 水域 (也就是 0 变成 1 )。
11+
12+
你想知道从矩阵最 上面 一行走到最 下面 一行,且只经过陆地格子的 最后一天 是哪一天。你可以从最上面一行的 任意 格子出发,到达最下面一行的 任意 格子。你只能沿着 四个 基本方向移动(也就是上下左右)。
13+
14+
请返回只经过陆地格子能从最 上面 一行走到最 下面 一行的 最后一天 。
15+
16+
 
17+
18+
示例 1:
19+
20+
输入:row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
21+
输出:2
22+
解释:上图描述了矩阵从第 0 天开始是如何变化的。
23+
可以从最上面一行到最下面一行的最后一天是第 2 天。
24+
25+
26+
示例 2:
27+
28+
输入:row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]
29+
输出:1
30+
解释:上图描述了矩阵从第 0 天开始是如何变化的。
31+
可以从最上面一行到最下面一行的最后一天是第 1 天。
32+
33+
34+
示例 3:
35+
36+
输入:row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]
37+
输出:3
38+
解释:上图描述了矩阵从第 0 天开始是如何变化的。
39+
可以从最上面一行到最下面一行的最后一天是第 3 天。
40+
41+
42+
 
43+
44+
提示:
45+
46+
2 <= row, col <= 2 * 104
47+
4 <= row * col <= 2 * 104
48+
cells.length == row * col
49+
1 <= ri <= row
50+
1 <= ci <= col
51+
cells 中的所有格子坐标都是 唯一 的。
52+
```
53+
54+
## 前置知识
55+
56+
- 多源 BFS
57+
- 二分
58+
59+
## 公司
60+
61+
- 暂无
62+
63+
## 思路
64+
65+
本题和 [1631. 最小体力消耗路径](./1631.path-with-minimum-effort.md) 类似。
66+
67+
由于:
68+
69+
- 如果第 n 天可以,那么小于 n 天都可以到达最后一行
70+
- 如果第 n 天不可以,那么大雨 n 天都无法到达最后一行
71+
72+
基于此,我们可以想到使用能力检测二分中的**最右二分**。而这里的能力检测,我们可以使用 DFS 或者 BFS。而由于起点可能有多个(第一行的所有陆地),因此使用**多源 BFS** 复杂度会更好,因此我们这里选择 BFS 来做。
73+
74+
本题还有一种并查集的解法,也非常有意思。具体可参考力扣中国的[官方题解](https://leetcode-cn.com/problems/last-day-where-you-can-still-cross/solution/ni-neng-chuan-guo-ju-zhen-de-zui-hou-yi-9j20y/) 的方法二。
75+
76+
## 代码
77+
78+
- 语言支持:Python3
79+
80+
Python3 Code:
81+
82+
```python
83+
84+
class Solution:
85+
def latestDayToCross(self, row: int, col: int, cells: List[List[int]]) -> int:
86+
def can(d):
87+
visited = set()
88+
q = collections.deque([(0,j) for j in range(col)])
89+
for x, y in cells[:d]:
90+
visited.add((x-1, y-1))
91+
while q:
92+
x,y = q.popleft()
93+
if (x,y) in visited: continue
94+
visited.add((x,y))
95+
if x == row - 1: return True
96+
for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]:
97+
if 0 <= x + dx < row and 0 <= y + dy < col: q.append((x+dx, y+dy))
98+
return False
99+
100+
l, r = 0, row * col
101+
while l <=r :
102+
mid = (l+r)//2
103+
if can(mid):
104+
l = mid + 1
105+
else:
106+
r = mid - 1
107+
return r
108+
109+
110+
```
111+
112+
**复杂度分析**
113+
114+
令 n 为 row 和 col 的乘积。
115+
116+
- 时间复杂度:$O(nlogn)$
117+
- 空间复杂度:$O(n)$
118+
119+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
120+
121+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
122+
123+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
124+
125+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
126+
127+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)