Skip to content

Commit f7e9312

Browse files
authored
Merge pull request #1045 from 0xff-dev/3243
Add solution and test-cases for problem 3243
2 parents a2736f7 + f6d3131 commit f7e9312

File tree

8 files changed

+71
-26
lines changed

8 files changed

+71
-26
lines changed
12.1 KB
Loading
14 KB
Loading
18.2 KB
Loading
11.3 KB
Loading
12.4 KB
Loading

leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [3243.Shortest Distance After Road Addition Queries I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer `n` and a 2D integer array `queries`.
5+
6+
There are `n` cities numbered from `0` to `n - 1`. Initially, there is a **unidirectional** road from city `i` to city `i + 1` for all `0 <= i < n - 1`.
7+
8+
`queries[i] = [ui, vi]` represents the addition of a new **unidirectional** road from city `ui` to city `vi`. After each query, you need to find the **length** of the **shortest path** from city `0` to city `n - 1`.
79

8-
**Example 1:**
10+
Return an array `answer` where for each `i` in the range `[0, queries.length - 1]`, `answer[i]` is the length of the shortest path from city `0` to city `n - 1` after processing the **first** `i + 1` queries.
11+
12+
**Example 1:**
13+
14+
![1](./1.jpg)
15+
![2](./2.jpg)
16+
![3](./3.jpg)
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: n = 5, queries = [[2,4],[0,2],[0,4]]
20+
Output: [3,2,1]
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
25+
![4](./4.jpg)
26+
![5](./5.jpg)
1927

20-
### 思路1
21-
> ...
22-
Shortest Distance After Road Addition Queries I
23-
```go
2428
```
29+
Input: n = 4, queries = [[0,3],[0,2]]
2530
31+
Output: [1,1]
32+
```
2633

2734
## 结语
2835

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int, queries [][]int) []int {
4+
ans := make([]int, len(queries))
5+
adj := make(map[int]map[int]struct{})
6+
for i := range n {
7+
adj[i] = make(map[int]struct{})
8+
}
9+
// 初始化路径
10+
for i := range n - 1 {
11+
adj[i][i+1] = struct{}{}
12+
}
13+
var bfs func() int
14+
bfs = func() int {
15+
queue := []int{0}
16+
step := 0
17+
used := map[int]struct{}{
18+
0: {},
19+
}
20+
for len(queue) > 0 {
21+
nq := make([]int, 0)
22+
for _, q := range queue {
23+
if q == n-1 {
24+
return step
25+
}
26+
for rel := range adj[q] {
27+
if _, ok := used[rel]; !ok {
28+
nq = append(nq, rel)
29+
used[rel] = struct{}{}
30+
}
31+
}
32+
}
33+
queue = nq
34+
step++
35+
}
36+
return -1
37+
}
38+
for i, q := range queries {
39+
adj[q[0]][q[1]] = struct{}{}
40+
ans[i] = bfs()
41+
}
42+
return ans
543
}

leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
n int
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, [][]int{{2, 4}, {0, 2}, {0, 4}}, []int{3, 2, 1}},
18+
{"TestCase2", 4, [][]int{{0, 3}, {0, 2}}, []int{1, 1}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.n, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.n, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)