Skip to content

Commit 464352f

Browse files
committed
improved reading
1 parent 0fbda09 commit 464352f

File tree

7 files changed

+28
-48
lines changed

7 files changed

+28
-48
lines changed

basic_algorithm/dp.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,16 @@ Function(x) {
134134
> 给定一个包含非负整数的  *m* x *n*  网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
135135
136136
思路:动态规划
137-
1、state: f[x][y]从起点走到 x,y 的最短路径
138-
2、function: f[x][y] = min(f[x-1][y], f[x][y-1]) + A[x][y]
139-
3、intialize: f[0][0] = A[0][0]、f[i][0] = sum(0,0 -> i,0)、 f[0][i] = sum(0,0 -> 0,i)
140-
4、answer: f[n-1][m-1]
137+
138+
1. state: f(x, y) 从起点走到 (x, y) 的最短路径
139+
140+
2. function: f(x, y) = min(f(x - 1, y), f(x, y - 1]) + A(x, y)
141+
142+
3. intialize: f(0, 0) = A(0, 0)、f(i, 0) = sum(0,0 -> i,0)、 f(0, i) = sum(0,0 -> 0,i)
143+
144+
4. answer: f(n - 1, m - 1)
145+
146+
5. 2D DP -> 1D DP
141147

142148
```Python
143149
class Solution:

basic_algorithm/graph/bfs_dfs.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def BFS(x):
8282

8383
> 给定一个二维矩阵,矩阵中元素 -1 表示墙或是障碍物,0 表示一扇门,INF (2147483647) 表示一个空的房间。你要给每个空房间位上填上该房间到最近门的距离,如果无法到达门,则填 INF 即可。
8484
85-
**图森面试真题**典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
85+
典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
8686

8787
```Python
8888
inf = 2147483647
@@ -137,7 +137,7 @@ class Solution:
137137
> 在给定的 01 矩阵 A 中,存在两座岛 (岛是由四面相连的 1 形成的一个连通分量)。现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。返回必须翻转的 0 的最小数目。
138138
>
139139
140-
**图森面试真题**思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
140+
思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
141141

142142
```Python
143143
class Solution:

basic_algorithm/graph/mst.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
> 地图上有 m 条无向边,每条边 (x, y, w) 表示位置 m 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?
66
7-
**图森面试真题**最小危险值为最小生成树中 0 到 n 路径上的最大边权。
7+
最小危险值为最小生成树中 0 到 n 路径上的最大边权。
88

99
```Python
1010
# Kruskal's algorithm

basic_algorithm/graph/shortest_path.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> 给定一个二维矩阵,矩阵中元素 -1 表示墙或是障碍物,0 表示一扇门,INF (2147483647) 表示一个空的房间。你要给每个空房间位上填上该房间到最近门的距离,如果无法到达门,则填 INF 即可。
1010
11-
**图森面试真题**典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
11+
典型的多源最短路径问题,将所有源作为 BFS 的第一层即可
1212

1313
```Python
1414
inf = 2147483647
@@ -62,7 +62,7 @@ class Solution:
6262

6363
> 在给定的 01 矩阵 A 中,存在两座岛 (岛是由四面相连的 1 形成的一个连通分量)。现在,我们可以将 0 变为 1,以使两座岛连接起来,变成一座岛。返回必须翻转的 0 的最小数目。
6464
65-
**图森面试真题**思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
65+
思路:DFS 遍历连通分量找边界,从边界开始 BFS找最短路径
6666

6767
```Python
6868
class Solution:

basic_algorithm/graph/topological_sorting.md

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
> 给定课程的先修关系,求一个可行的修课顺序
88
9-
**图森面试真题**。非常经典的拓扑排序应用题目。下面给出 3 种实现方法,可以当做模板使用。
10-
11-
12-
13-
方法 1:DFS 的递归实现
9+
非常经典的拓扑排序应用题目。下面给出 3 种实现方法,可以当做模板使用。
1410

1511
```Python
12+
# 方法 1:DFS 的递归实现
1613
NOT_VISITED = 0
1714
DISCOVERING = 1
1815
VISITED = 2
@@ -45,9 +42,8 @@ class Solution:
4542
return tsort_rev[::-1]
4643
```
4744

48-
方法 2:DFS 的迭代实现
49-
5045
```Python
46+
# 方法 2:DFS 的迭代实现
5147
NOT_VISITED = 0
5248
DISCOVERING = 1
5349
VISITED = 2
@@ -85,9 +81,8 @@ class Solution:
8581
return tsort_rev[::-1]
8682
```
8783

88-
方法 3:[Kahn's algorithm](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm)
89-
9084
```Python
85+
# 方法 3:Kahn's algorithm: https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
9186
class Solution:
9287
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
9388

data_structure/heap.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Heap 可以高效地取出或更新当前池中优先级最高的元素,因此
9191
> 公司有 n 个工程师,给两个数组 speed 和 efficiency,其中 speed[i] 和 efficiency[i] 分别代表第 i 位工程师的速度和效率。请你返回由最多 k 个工程师组成的团队的最大表现值。表现值的定义为:一个团队中所有工程师速度的和乘以他们效率值中的最小值。
9292
>
9393
94-
**图森面试真题**[See my review here.](https://leetcode.com/problems/maximum-performance-of-a-team/discuss/741822/Met-this-problem-in-my-interview!!!-(Python3-greedy-with-heap)) [或者这里(中文)](https://leetcode-cn.com/problems/maximum-performance-of-a-team/solution/greedy-with-min-heap-lai-zi-zhen-shi-mian-shi-de-j/)
94+
[See my review here.](https://leetcode.com/problems/maximum-performance-of-a-team/discuss/741822/Met-this-problem-in-my-interview!!!-(Python3-greedy-with-heap)) [或者这里(中文)](https://leetcode-cn.com/problems/maximum-performance-of-a-team/solution/greedy-with-min-heap-lai-zi-zhen-shi-mian-shi-de-j/)
9595

9696
```Python
9797
class Solution:
@@ -116,7 +116,7 @@ class Solution:
116116

117117
### [ipo](https://leetcode-cn.com/problems/ipo/)
118118

119-
**图森面试真题**贪心策略为每次做当前成本范围内利润最大的项目。
119+
贪心策略为每次做当前成本范围内利润最大的项目。
120120

121121
```Python
122122
class Solution:
@@ -143,7 +143,7 @@ class Solution:
143143

144144
### [meeting-rooms-ii](https://leetcode-cn.com/problems/meeting-rooms-ii/)
145145

146-
**图森面试真题**此题用 greedy + heap 解并不是很 intuitive,存在复杂度相同但更简单直观的做法。
146+
此题用 greedy + heap 解并不是很 intuitive,存在复杂度相同但更简单直观的做法。
147147

148148
```Python
149149
class Solution:
@@ -207,7 +207,7 @@ class Solution:
207207

208208
> 地图上有 m 条无向边,每条边 (x, y, w) 表示位置 m 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?
209209
210-
**图森面试真题**最小危险值为最小生成树中 0 到 n 路径上的最大边权。
210+
最小危险值为最小生成树中 0 到 n 路径上的最大边权。
211211

212212
```Python
213213
class Solution:

data_structure/union_find.md

+5-26
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ class Solution:
102102

103103
> 地图上有 m 条无向边,每条边 (x, y, w) 表示位置 m 到位置 y 的权值为 w。从位置 0 到 位置 n 可能有多条路径。我们定义一条路径的危险值为这条路径中所有的边的最大权值。请问从位置 0 到 位置 n 所有路径中最小的危险值为多少?
104104
105-
**图森面试真题**最小危险值为最小生成树中 0 到 n 路径上的最大边权。
105+
最小危险值为最小生成树中 0 到 n 路径上的最大边权。
106106

107107
```Python
108108
# Kruskal's algorithm
109109
class Solution:
110110
def getMinRiskValue(self, N, M, X, Y, W):
111111

112-
# Kruskal's algorithm with union-find to construct MST
112+
# Kruskal's algorithm with union-find
113113
parent = list(range(N + 1))
114114

115115
def find(x):
@@ -127,28 +127,7 @@ class Solution:
127127

128128
edges = sorted(zip(W, X, Y))
129129

130-
MST_edges = []
131-
for edge in edges:
132-
if union(edge[1], edge[2]):
133-
MST_edges.append(edge)
134-
if find(0) == find(N):
135-
break
136-
137-
MST = collections.defaultdict(list)
138-
target = find(0)
139-
for w, u, v in MST_edges:
140-
if find(u) == target and find(v) == target:
141-
MST[u].append((v, w))
142-
MST[v].append((u, w))
143-
144-
# dfs to search route from 0 to n
145-
dfs = [(0, None, float('-inf'))]
146-
while dfs:
147-
v, p, max_w = dfs.pop()
148-
for n, w in MST[v]:
149-
cur_max_w = max(max_w, w)
150-
if n == N:
151-
return cur_max_w
152-
if n != p:
153-
dfs.append((n, v, cur_max_w))
130+
for w, x, y in edges:
131+
if union(x, y) and find(0) == find(N): # early return without constructing MST
132+
return w
154133
```

0 commit comments

Comments
 (0)