Skip to content

Commit c3b23ba

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.1466 (doocs#2886)
* feat: add iterative TS solution to lc problem: No.1466 * Update README.md * Update README_EN.md * Update Solution2.ts * Create Solution2.py * Create Solution2.java * Create Solution2.cpp * Create Solution2.go * style: format code and docs with prettier --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 282001a commit c3b23ba

File tree

7 files changed

+422
-0
lines changed

7 files changed

+422
-0
lines changed

solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README.md

+151
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,155 @@ impl Solution {
230230

231231
<!-- solution:end -->
232232

233+
<!-- solution:start -->
234+
235+
### 方法二:BFS
236+
237+
我们可以使用广度优先搜索的方法,从节点 $0$ 出发,搜索其他所有节点,过程中,如果遇到需要变更方向的边,则累加一次变更方向的次数。
238+
239+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是题目中节点的数量。
240+
241+
<!-- tabs:start -->
242+
243+
#### Python3
244+
245+
```python
246+
class Solution:
247+
def minReorder(self, n: int, connections: List[List[int]]) -> int:
248+
g = [[] for _ in range(n)]
249+
for a, b in connections:
250+
g[a].append((b, 1))
251+
g[b].append((a, 0))
252+
q = deque([0])
253+
vis = {0}
254+
ans = 0
255+
while q:
256+
a = q.popleft()
257+
for b, c in g[a]:
258+
if b not in vis:
259+
vis.add(b)
260+
q.append(b)
261+
ans += c
262+
return ans
263+
```
264+
265+
```java
266+
class Solution {
267+
public int minReorder(int n, int[][] connections) {
268+
List<int[]>[] g = new List[n];
269+
Arrays.setAll(g, k -> new ArrayList<>());
270+
for (var e : connections) {
271+
int a = e[0], b = e[1];
272+
g[a].add(new int[] {b, 1});
273+
g[b].add(new int[] {a, 0});
274+
}
275+
Deque<Integer> q = new ArrayDeque<>();
276+
q.offer(0);
277+
boolean[] vis = new boolean[n];
278+
vis[0] = true;
279+
int ans = 0;
280+
while (!q.isEmpty()) {
281+
int a = q.poll();
282+
for (var e : g[a]) {
283+
int b = e[0], c = e[1];
284+
if (!vis[b]) {
285+
vis[b] = true;
286+
q.offer(b);
287+
ans += c;
288+
}
289+
}
290+
}
291+
return ans;
292+
}
293+
}
294+
```
295+
296+
```cpp
297+
class Solution {
298+
public:
299+
int minReorder(int n, vector<vector<int>>& connections) {
300+
vector<pair<int, int>> g[n];
301+
for (auto& e : connections) {
302+
int a = e[0], b = e[1];
303+
g[a].emplace_back(b, 1);
304+
g[b].emplace_back(a, 0);
305+
}
306+
queue<int> q{{0}};
307+
vector<bool> vis(n);
308+
vis[0] = true;
309+
int ans = 0;
310+
while (q.size()) {
311+
int a = q.front();
312+
q.pop();
313+
for (auto& [b, c] : g[a]) {
314+
if (!vis[b]) {
315+
vis[b] = true;
316+
q.push(b);
317+
ans += c;
318+
}
319+
}
320+
}
321+
return ans;
322+
}
323+
};
324+
```
325+
326+
```go
327+
func minReorder(n int, connections [][]int) (ans int) {
328+
g := make([][][2]int, n)
329+
for _, e := range connections {
330+
a, b := e[0], e[1]
331+
g[a] = append(g[a], [2]int{b, 1})
332+
g[b] = append(g[b], [2]int{a, 0})
333+
}
334+
q := []int{0}
335+
vis := make([]bool, n)
336+
vis[0] = true
337+
for len(q) > 0 {
338+
a := q[0]
339+
q = q[1:]
340+
for _, e := range g[a] {
341+
b, c := e[0], e[1]
342+
if !vis[b] {
343+
vis[b] = true
344+
q = append(q, b)
345+
ans += c
346+
}
347+
}
348+
}
349+
return
350+
}
351+
```
352+
353+
```ts
354+
function minReorder(n: number, connections: number[][]): number {
355+
const g: [number, number][][] = Array.from({ length: n }, () => []);
356+
for (const [a, b] of connections) {
357+
g[a].push([b, 1]);
358+
g[b].push([a, 0]);
359+
}
360+
361+
const q: number[] = [0];
362+
const vis = new Set<number>();
363+
vis.add(0);
364+
365+
let ans = 0;
366+
while (q.length) {
367+
const a = q.pop()!;
368+
for (const [b, c] of g[a]) {
369+
if (!vis.has(b)) {
370+
vis.add(b);
371+
q.push(b);
372+
ans += c;
373+
}
374+
}
375+
}
376+
return ans;
377+
}
378+
```
379+
380+
<!-- tabs:end -->
381+
382+
<!-- solution:end -->
383+
233384
<!-- problem:end -->

solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/README_EN.md

+151
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,155 @@ impl Solution {
229229

230230
<!-- solution:end -->
231231

232+
<!-- solution:start -->
233+
234+
### Solution 2: BFS
235+
236+
We can use the Breadth-First Search (BFS) method, starting from node $0$, to search all other nodes. During the process, if we encounter an edge that requires a change of direction, we increment the count of direction changes.
237+
238+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the problem.
239+
240+
<!-- tabs:start -->
241+
242+
#### Python3
243+
244+
```python
245+
class Solution:
246+
def minReorder(self, n: int, connections: List[List[int]]) -> int:
247+
g = [[] for _ in range(n)]
248+
for a, b in connections:
249+
g[a].append((b, 1))
250+
g[b].append((a, 0))
251+
q = deque([0])
252+
vis = {0}
253+
ans = 0
254+
while q:
255+
a = q.popleft()
256+
for b, c in g[a]:
257+
if b not in vis:
258+
vis.add(b)
259+
q.append(b)
260+
ans += c
261+
return ans
262+
```
263+
264+
```java
265+
class Solution {
266+
public int minReorder(int n, int[][] connections) {
267+
List<int[]>[] g = new List[n];
268+
Arrays.setAll(g, k -> new ArrayList<>());
269+
for (var e : connections) {
270+
int a = e[0], b = e[1];
271+
g[a].add(new int[] {b, 1});
272+
g[b].add(new int[] {a, 0});
273+
}
274+
Deque<Integer> q = new ArrayDeque<>();
275+
q.offer(0);
276+
boolean[] vis = new boolean[n];
277+
vis[0] = true;
278+
int ans = 0;
279+
while (!q.isEmpty()) {
280+
int a = q.poll();
281+
for (var e : g[a]) {
282+
int b = e[0], c = e[1];
283+
if (!vis[b]) {
284+
vis[b] = true;
285+
q.offer(b);
286+
ans += c;
287+
}
288+
}
289+
}
290+
return ans;
291+
}
292+
}
293+
```
294+
295+
```cpp
296+
class Solution {
297+
public:
298+
int minReorder(int n, vector<vector<int>>& connections) {
299+
vector<pair<int, int>> g[n];
300+
for (auto& e : connections) {
301+
int a = e[0], b = e[1];
302+
g[a].emplace_back(b, 1);
303+
g[b].emplace_back(a, 0);
304+
}
305+
queue<int> q{{0}};
306+
vector<bool> vis(n);
307+
vis[0] = true;
308+
int ans = 0;
309+
while (q.size()) {
310+
int a = q.front();
311+
q.pop();
312+
for (auto& [b, c] : g[a]) {
313+
if (!vis[b]) {
314+
vis[b] = true;
315+
q.push(b);
316+
ans += c;
317+
}
318+
}
319+
}
320+
return ans;
321+
}
322+
};
323+
```
324+
325+
```go
326+
func minReorder(n int, connections [][]int) (ans int) {
327+
g := make([][][2]int, n)
328+
for _, e := range connections {
329+
a, b := e[0], e[1]
330+
g[a] = append(g[a], [2]int{b, 1})
331+
g[b] = append(g[b], [2]int{a, 0})
332+
}
333+
q := []int{0}
334+
vis := make([]bool, n)
335+
vis[0] = true
336+
for len(q) > 0 {
337+
a := q[0]
338+
q = q[1:]
339+
for _, e := range g[a] {
340+
b, c := e[0], e[1]
341+
if !vis[b] {
342+
vis[b] = true
343+
q = append(q, b)
344+
ans += c
345+
}
346+
}
347+
}
348+
return
349+
}
350+
```
351+
352+
```ts
353+
function minReorder(n: number, connections: number[][]): number {
354+
const g: [number, number][][] = Array.from({ length: n }, () => []);
355+
for (const [a, b] of connections) {
356+
g[a].push([b, 1]);
357+
g[b].push([a, 0]);
358+
}
359+
360+
const q: number[] = [0];
361+
const vis = new Set<number>();
362+
vis.add(0);
363+
364+
let ans = 0;
365+
while (q.length) {
366+
const a = q.pop()!;
367+
for (const [b, c] of g[a]) {
368+
if (!vis.has(b)) {
369+
vis.add(b);
370+
q.push(b);
371+
ans += c;
372+
}
373+
}
374+
}
375+
return ans;
376+
}
377+
```
378+
379+
<!-- tabs:end -->
380+
381+
<!-- solution:end -->
382+
232383
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int minReorder(int n, vector<vector<int>>& connections) {
4+
vector<pair<int, int>> g[n];
5+
for (auto& e : connections) {
6+
int a = e[0], b = e[1];
7+
g[a].emplace_back(b, 1);
8+
g[b].emplace_back(a, 0);
9+
}
10+
queue<int> q{{0}};
11+
vector<bool> vis(n);
12+
vis[0] = true;
13+
int ans = 0;
14+
while (q.size()) {
15+
int a = q.front();
16+
q.pop();
17+
for (auto& [b, c] : g[a]) {
18+
if (!vis[b]) {
19+
vis[b] = true;
20+
q.push(b);
21+
ans += c;
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func minReorder(n int, connections [][]int) (ans int) {
2+
g := make([][][2]int, n)
3+
for _, e := range connections {
4+
a, b := e[0], e[1]
5+
g[a] = append(g[a], [2]int{b, 1})
6+
g[b] = append(g[b], [2]int{a, 0})
7+
}
8+
q := []int{0}
9+
vis := make([]bool, n)
10+
vis[0] = true
11+
for len(q) > 0 {
12+
a := q[0]
13+
q = q[1:]
14+
for _, e := range g[a] {
15+
b, c := e[0], e[1]
16+
if !vis[b] {
17+
vis[b] = true
18+
q = append(q, b)
19+
ans += c
20+
}
21+
}
22+
}
23+
return
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int minReorder(int n, int[][] connections) {
3+
List<int[]>[] g = new List[n];
4+
Arrays.setAll(g, k -> new ArrayList<>());
5+
for (var e : connections) {
6+
int a = e[0], b = e[1];
7+
g[a].add(new int[] {b, 1});
8+
g[b].add(new int[] {a, 0});
9+
}
10+
Deque<Integer> q = new ArrayDeque<>();
11+
q.offer(0);
12+
boolean[] vis = new boolean[n];
13+
vis[0] = true;
14+
int ans = 0;
15+
while (!q.isEmpty()) {
16+
int a = q.poll();
17+
for (var e : g[a]) {
18+
int b = e[0], c = e[1];
19+
if (!vis[b]) {
20+
vis[b] = true;
21+
q.offer(b);
22+
ans += c;
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
}

0 commit comments

Comments
 (0)