From 87f03744cb8d930ae72edf621d8ea71e09ac3b10 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Fri, 9 Sep 2022 17:10:08 +0800 Subject: [PATCH 1/5] =?UTF-8?q?Update=200323.=20=E6=97=A0=E5=90=91?= =?UTF-8?q?=E5=9B=BE=E4=B8=AD=E8=BF=9E=E9=80=9A=E5=88=86=E9=87=8F=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=9B=AE.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17347円232円204円346円225円260円347円233円256円.md" | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git "a/Solutions/0323. 346円227円240円345円220円221円345円233円276円344円270円255円350円277円236円351円200円232円345円210円206円351円207円217円347円232円204円346円225円260円347円233円256円.md" "b/Solutions/0323. 346円227円240円345円220円221円345円233円276円344円270円255円350円277円236円351円200円232円345円210円206円351円207円217円347円232円204円346円225円260円347円233円256円.md" index 11f10b53..77bb433d 100644 --- "a/Solutions/0323. 346円227円240円345円220円221円345円233円276円344円270円255円350円277円236円351円200円232円345円210円206円351円207円217円347円232円204円346円225円260円347円233円256円.md" +++ "b/Solutions/0323. 346円227円240円345円220円221円345円233円276円344円270円255円350円277円236円351円200円232円345円210円206円351円207円217円347円232円204円346円225円260円347円233円256円.md" @@ -37,6 +37,14 @@ ## 解题思路 +先来看一下图论中相关的名次解释。 + +- **连通图**:在无向图中,如果可以从顶点 $v_i$ 到达 $v_j,ドル则称 $v_i$ 和 $v_j$ 连通。如果图中任意两个顶点之间都连通,则称该图为连通图。 +- **无向图的连通分量**:如果该图为连通图,则连通分量为本身;否则将无向图中的极大连通子图称为连通分量,每个连通分量都是一个连通图。 +- **无向图的连通分量个数**:无向图的极大连通子图的个数。 + +接下来我们来解决这道题。 + ### 思路 1:深度优先搜索 1. 使用 `visited` 数组标记遍历过的节点,使用 `count` 记录连通分量数量。 @@ -73,6 +81,51 @@ class Solution: ### 思路 1:复杂度分析 -- **时间复杂度**:$O(v)$。其中 $v$ 是顶点个数。 -- **空间复杂度**:$O(v)$。 +- **时间复杂度**:$O(n)$。其中$n$ 是顶点个数。 +- **空间复杂度**:$O(n)$。 + +### 思路 2:广度优先搜索 + +1. 使用变量 `count` 记录连通分量个数。使用集合变量 `visited` 记录访问过的节点,使用邻接表 `graph` 记录图结构。 +2. 从 `0` 开始,依次遍历 `n` 个节点。 +3. 如果第 `i` 个节点未访问过: + 1. 将其添加到 `visited` 中。 + 2. 并且连通分量个数累加,即 `count += 1`。 + 3. 定义一个队列 `queue`,将第 `i` 个节点加入到队列中。 + 4. 从队列中取出第一个节点,遍历与其链接的节点,并将未遍历过的节点加入到队列 `queue` 和 `visited` 中。 + 5. 直到队列为空,则继续向后遍历。 +4. 最后输出连通分量数目 `count`。 + +### 思路 2:代码 + +```Python +import collections + +class Solution: + def countComponents(self, n: int, edges: List[List[int]]) -> int: + count = 0 + visited = set() + graph = [[] for _ in range(n)] + + for x, y in edges: + graph[x].append(y) + graph[y].append(x) + + for i in range(n): + if i not in visited: + visited.add(i) + count += 1 + queue = collections.deque([i]) + while queue: + node_u = queue.popleft() + for node_v in graph[node_u]: + if node_v not in visited: + visited.add(node_v) + queue.append(node_v) + return count +``` + +### 思路 2:复杂度分析 +- **时间复杂度**:$O(n)$。其中$n$ 是顶点个数。 +- **空间复杂度**:$O(n)$。 From 8069239f62f14f9f0981573c0ab54520a03a3c91 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Fri, 9 Sep 2022 17:10:13 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Update=200416.=20=E5=88=86=E5=89=B2?= =?UTF-8?q?=E7=AD=89=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Solutions/0416. 345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" "b/Solutions/0416. 345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" index d159bec8..75672bd1 100644 --- "a/Solutions/0416. 345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" +++ "b/Solutions/0416. 345円210円206円345円211円262円347円255円211円345円222円214円345円255円220円351円233円206円.md" @@ -15,7 +15,7 @@ 转换为一维 dp 就是:`dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])`。 -然后进行递归求解。最后判断 `dp[target]` 和 `target` 是否相等即可。 +然后进行递推求解。最后判断 `dp[target]` 和 `target` 是否相等即可。 ## 代码 From b47495a0e28698551e18c9a9da32c5cc80aa1b06 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Fri, 9 Sep 2022 17:10:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?Update=200463.=20=E5=B2=9B=E5=B1=BF?= =?UTF-8?q?=E7=9A=84=E5=91=A8=E9=95=BF.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77347円232円204円345円221円250円351円225円277円.md" | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git "a/Solutions/0463. 345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" "b/Solutions/0463. 345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" index 148d36e3..df3bf897 100644 --- "a/Solutions/0463. 345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" +++ "b/Solutions/0463. 345円262円233円345円261円277円347円232円204円345円221円250円351円225円277円.md" @@ -5,21 +5,38 @@ ## 题目大意 -给定一个 `row * col` 大小的二维网格地图 `grid` ,其中:`grid[i][j] = 1` 表示陆地,`grid[i][j] = 0` 表示水域。 +**描述**:给定一个 `row * col` 大小的二维网格地图 `grid` ,其中:`grid[i][j] = 1` 表示陆地,`grid[i][j] = 0` 表示水域。 网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(多个表示陆地的格子相连组成)。 岛屿内部中没有「湖」(指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。 -要求:计算这个岛屿的周长。 +**要求**:计算这个岛屿的周长。 -示例:下图周长为 16。 +**说明**: + +- $row == grid.length$。 +- $col == grid[i].length$。 +- 1ドル <= row, col <= 100$。 +- $grid[i][j]$ 为 0ドル$ 或 1ドル$。 + +**示例**: ![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/island.png) +```Python +输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] +输出:16 +解释:它的周长是上面图片中的 16 个黄色的边 + + +输入:grid = [[1]] +输出:4 +``` + ## 解题思路 -可以使用广度优先搜索求解。具体做法如下: +### 思路 1:广度优先搜索 1. 使用整形变量 `count` 存储周长,使用队列 `queue` 用于进行广度优先搜索。 2. 遍历一遍二维数组 `grid`,对 `grid[row][col] == 1` 的区域进行广度优先搜索。 @@ -29,7 +46,7 @@ 6. 如果相邻区域 `grid[new_row][new_col] == 1`,则将其赋值为 `2`,并将坐标加入队列。 7. 继续执行 4 ~ 6 步,直到队列为空时返回 `count`。 -## 代码 +### 思路 1:代码 ```Python class Solution: @@ -63,6 +80,11 @@ class Solution: return self.bfs(grid, rows, cols, row, col) ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(n \times m),ドル其中 $m$ 和 $n$ 分别为行数和列数。 +- **空间复杂度**:$O(n \times m)$。 + ## 参考资料 - 【题解】[Golang BFS 实现,性能比dfs要高 - 岛屿的周长 - 力扣](https://leetcode.cn/problems/island-perimeter/solution/golang-bfs-shi-xian-xing-neng-bi-dfsyao-nln2g/) From daecb5e58c0891c8ed4ade1a21c4bfe7e1f98866 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Fri, 9 Sep 2022 17:10:19 +0800 Subject: [PATCH 4/5] =?UTF-8?q?Update=200752.=20=E6=89=93=E5=BC=80?= =?UTF-8?q?=E8=BD=AC=E7=9B=98=E9=94=81.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00350円275円254円347円233円230円351円224円201円.md" | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git "a/Solutions/0752. 346円211円223円345円274円200円350円275円254円347円233円230円351円224円201円.md" "b/Solutions/0752. 346円211円223円345円274円200円350円275円254円347円233円230円351円224円201円.md" index b2e2b3d7..fd4e4dce 100644 --- "a/Solutions/0752. 346円211円223円345円274円200円350円275円254円347円233円230円351円224円201円.md" +++ "b/Solutions/0752. 346円211円223円345円274円200円350円275円254円347円233円230円351円224円201円.md" @@ -5,27 +5,53 @@ ## 题目大意 -有一把带有四个数字的密码锁,每个位置上有 `0` ~ `9` 共 `10` 个数字。每次只能将其中一个位置上的数字转动一下。可以向上转,也可以向下转。比如:`1 -> 2`、`2 -> 1`。 +**描述**:有一把带有四个数字的密码锁,每个位置上有 `0` ~ `9` 共 `10` 个数字。每次只能将其中一个位置上的数字转动一下。可以向上转,也可以向下转。比如:`1 -> 2`、`2 -> 1`。 密码锁的初始数字为:`0000`。现在给定一组表示死亡数字的字符串数组 `deadends`,和一个带有四位数字的目标字符串 `target`。 如果密码锁转动到 `deadends` 中任一字符串状态,则锁就会永久锁定,无法再次旋转。 -要求:给出最小的选择次数,使得锁的状态由 `0000` 转动到 `target`。 +**要求**:给出使得锁的状态由 `0000` 转动到 `target` 的最小的选择次数。如果无论如何不能解锁,返回 `-1` 。 -## 解题思路 +**说明**: + +- 1ドル \le deadends.length \le 500$ + $deadends[i].length == 4$ + $target.length == 4$ + $target$ 不在 $deadends$ 之中 + $target$ 和 $deadends[i]$ 仅由若干位数字组成。 + +**示例**: + +```Python +输入:deadends = ["0201","0101","0102","1212","2002"], target = "0202" +输出:6 +解释: +可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。 +注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的, +因为当拨动到 "0102" 时这个锁就会被锁定。 + +输入: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888" +输出:-1 +解释:无法旋转到目标数字且不被锁定。 +``` -使用广度优先搜索遍历,将`0000` 状态入队。 +## 解题思路 -- 将队列中的元素出队,判断是否为死亡字符串 -- 如果为死亡字符串,则跳过该状态,否则继续执行。 +### 思路 1:广度优先搜索 -- 如果为目标字符串,则返回当前路径长度,否则继续执行。 -- 枚举当前状态所有位置所能到达的所有状态,并判断是否访问过该状态。 +1. 定义 `visited` 为标记访问节点的 set 集合变量,`queue` 为存放节点的队列。 +2. 将`0000` 状态标记为访问,并将其加入队列 `queue`。 +3. 将当前队列中的所有状态依次出队,判断这些状态是否为死亡字符串。 + 1. 如果为死亡字符串,则跳过该状态,否则继续执行。 + 2. 如果为目标字符串,则返回当前路径长度,否则继续执行。 -- 如果之前出现过该状态,则继续执行,否则将其存入队列,并标记访问。 +4. 枚举当前状态所有位置所能到达的所有状态(通过向上或者向下旋转),并判断是否访问过该状态。 +5. 如果之前出现过该状态,则继续执行,否则将其存入队列,并标记访问。 +6. 遍历完步骤 3 中当前队列中的所有状态,令路径长度加 `1`,继续执行 3 ~ 5 步,直到队列为空。 +7. 如果队列为空,也未能到达目标状态,则返回 `-1`。 -## 代码 +### 思路 1:代码 ```Python import collections @@ -73,3 +99,7 @@ class Solution: return "".join(s_list) ``` +### 思路 1:复杂度分析 + +- **时间复杂度**:$O(10^d \times d^2 + m \times d)$。其中 $d$ 是数字的位数,$m$ 是数组 $deadends$ 的长度。 +- **空间复杂度**:$O(10^D \times d + m)$。 From decd26caf27fb645d9f04b112c2411ae8af1b5a6 Mon Sep 17 00:00:00 2001 From: ITCharge Date: Fri, 9 Sep 2022 17:10:28 +0800 Subject: [PATCH 5/5] Update Course-Git-02.md --- Assets/Course/Course-Git-02.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Assets/Course/Course-Git-02.md b/Assets/Course/Course-Git-02.md index 3b599606..0fae1d58 100644 --- a/Assets/Course/Course-Git-02.md +++ b/Assets/Course/Course-Git-02.md @@ -30,16 +30,13 @@ - [0234. 回文链表](https://leetcode.cn/problems/palindrome-linked-list/) - [0138. 复制带随机指针的链表](https://leetcode.cn/problems/copy-list-with-random-pointer/) - [更多链表基础题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/02.Linked-List/01.Linked-List-Basic/10.Linked-List-Basic-List.md) - - 第 03 天学习内容: - [链表排序](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/02.Linked-List/02.Linked-List-Sort/01.Linked-List-Sort.md) - 第 03 天课程题目: - [0147. 对链表进行插入排序](https://leetcode.cn/problems/insertion-sort-list/) - [0021. 合并两个有序链表](https://leetcode.cn/problems/merge-two-sorted-lists/) - [0148. 排序链表](https://leetcode.cn/problems/sort-list/) - - [更多链表排序题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/02.Linked-List/02.Linked-List-Sort/10.Linked-List-Sort-List.md) - - 第 04 天学习内容: - [链表双指针](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/02.Linked-List/03.Linked-List-Two-Pointers/01.Linked-List-Two-Pointers.md) - 第 04 天课程题目: @@ -61,7 +58,6 @@ - [0394. 字符串解码](https://leetcode.cn/problems/decode-string/) - [0946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) - [更多堆栈基础知识相关题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/03.Stack/01.Stack-Basic/10.Stack-Basic-List.md) - - 第 07 天学习内容: - [单调栈](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/03.Stack/03.Monotone-Stack/01.Monotone-Stack.md) - 第 07 天课程题目: @@ -73,7 +69,7 @@ ### Task 03 深度优先搜索(第 08 ~ 10 天) - 第 08 ~ 10 天学习内容: - - [深度优先搜索](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/03.Stack/02.Stack-DFS/01.Stack-DFS.md) + - [深度优先搜索](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/08.Graph/02.Graph-Traversal/01.Graph-DFS.md) - 第 08 天课程题目: - [0200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/) - [0133. 克隆图](https://leetcode.cn/problems/clone-graph/) @@ -86,7 +82,7 @@ - [0417. 太平洋大西洋水流问题](https://leetcode.cn/problems/pacific-atlantic-water-flow/) - [1020. 飞地的数量](https://leetcode.cn/problems/number-of-enclaves/) - [1254. 统计封闭岛屿的数目](https://leetcode.cn/problems/number-of-closed-islands/) -- [更多栈与深度优先搜索题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/03.Stack/02.Stack-DFS/10.Stack-DFS-List.md) +- [更多深度优先搜索题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/08.Graph/02.Graph-Traversal/02.Graph-DFS-List.md) ### Task 04:队列与优先队列(第 11 ~ 12 天) @@ -97,7 +93,6 @@ - [剑指 Offer II 041. 滑动窗口的平均值](https://leetcode.cn/problems/qIsx9U/) - [0225. 用队列实现栈](https://leetcode.cn/problems/implement-stack-using-queues/) - [更多队列基础题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/04.Queue/01.Queue-Basic/10.Queue-Basic-List.md) - - 第 12 天学习内容: - [优先队列](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/04.Queue/03.Priority-Queue/01.Priority-Queue.md) - 第 12 天课程题目: @@ -109,7 +104,7 @@ ### Task 05 广度优先搜索(第 13 ~ 14 天) - 第 13 ~ 14 天学习内容: - - [广度优先搜索](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/04.Queue/02.Queue-BFS/01.Queue-BFS.md) + - [广度优先搜索](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/08.Graph/02.Graph-Traversal/03.Graph-BFS.md) - 第 13 天课程题目: - [0463. 岛屿的周长](https://leetcode.cn/problems/island-perimeter/) - [0752. 打开转盘锁](https://leetcode.cn/problems/open-the-lock/) @@ -118,7 +113,7 @@ - [0542. 01 矩阵](https://leetcode.cn/problems/01-matrix/) - [0322. 零钱兑换](https://leetcode.cn/problems/coin-change/) - [剑指 Offer 13. 机器人的运动范围](https://leetcode.cn/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/) -- [更多广度优先搜索题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/04.Queue/02.Queue-BFS/10.Queue-BFS-List.md) +- [更多广度优先搜索题目](https://github.com/itcharge/LeetCode-Py/blob/main/Contents/08.Graph/02.Graph-Traversal/04.Graph-BFS-List.md) ### Task 06 拓扑排序(第 15 天)

AltStyle によって変換されたページ (->オリジナル) /