diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/1.jpg b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/1.jpg new file mode 100644 index 000000000..0b52cf4fe Binary files /dev/null and b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/1.jpg differ diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/2.jpg b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/2.jpg new file mode 100644 index 000000000..37f22b08d Binary files /dev/null and b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/2.jpg differ diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/3.jpg b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/3.jpg new file mode 100644 index 000000000..9c8da128c Binary files /dev/null and b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/3.jpg differ diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/4.jpg b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/4.jpg new file mode 100644 index 000000000..7e60e8bf8 Binary files /dev/null and b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/4.jpg differ diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/5.jpg b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/5.jpg new file mode 100644 index 000000000..636661d7b Binary files /dev/null and b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/5.jpg differ diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/README.md b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/README.md index 5a0cded4c..0b1f861ae 100755 --- a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/README.md +++ b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/README.md @@ -1,28 +1,35 @@ # [3243.Shortest Distance After Road Addition Queries I][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer `n` and a 2D integer array `queries`. + +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`. + +`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`. -**Example 1:** +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. + +**Example 1:** + +![1](./1.jpg) +![2](./2.jpg) +![3](./3.jpg) ``` -Input: a = "11", b = "1" -Output: "100" +Input: n = 5, queries = [[2,4],[0,2],[0,4]] +Output: [3,2,1] ``` -## 题意 -> ... +**Example 2:** -## 题解 +![4](./4.jpg) +![5](./5.jpg) -### 思路1 -> ... -Shortest Distance After Road Addition Queries I -```go ``` +Input: n = 4, queries = [[0,3],[0,2]] +Output: [1,1] +``` ## 结语 diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution.go b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution.go index d115ccf5e..294d6fef7 100644 --- a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution.go +++ b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(n int, queries [][]int) []int { + ans := make([]int, len(queries)) + adj := make(map[int]map[int]struct{}) + for i := range n { + adj[i] = make(map[int]struct{}) + } + // 初始化路径 + for i := range n - 1 { + adj[i][i+1] = struct{}{} + } + var bfs func() int + bfs = func() int { + queue := []int{0} + step := 0 + used := map[int]struct{}{ + 0: {}, + } + for len(queue) > 0 { + nq := make([]int, 0) + for _, q := range queue { + if q == n-1 { + return step + } + for rel := range adj[q] { + if _, ok := used[rel]; !ok { + nq = append(nq, rel) + used[rel] = struct{}{} + } + } + } + queue = nq + step++ + } + return -1 + } + for i, q := range queries { + adj[q[0]][q[1]] = struct{}{} + ans[i] = bfs() + } + return ans } diff --git a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution_test.go b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution_test.go index 14ff50eb4..f72e9a175 100644 --- a/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution_test.go +++ b/leetcode/3201-3300/3243.Shortest-Distance-After-Road-Addition-Queries-I/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + n int + queries [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, [][]int{{2, 4}, {0, 2}, {0, 4}}, []int{3, 2, 1}}, + {"TestCase2", 4, [][]int{{0, 3}, {0, 2}}, []int{1, 1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.queries) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }