Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f524a9a

Browse files
feat: add solutions to lc problem: No.3243 (doocs#3363)
No.3243.Shortest Distance After Road Addition Queries I
1 parent 1b3cee5 commit f524a9a

File tree

14 files changed

+536
-17
lines changed

14 files changed

+536
-17
lines changed

‎solution/0600-0699/0600.Non-negative Integers without Consecutive Ones/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tags:
2525
<pre>
2626
<strong>输入:</strong> n = 5
2727
<strong>输出:</strong> 5
28-
<strong>解释:</strong>
28+
<strong>解释:</strong>
2929
下面列出范围在 [0, 5] 的非负整数与其对应的二进制表示:
3030
0 : 0
3131
1 : 1

‎solution/0600-0699/0600.Non-negative Integers without Consecutive Ones/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Here are the non-negative integers &lt;= 5 with their corresponding binary repre
3232
3 : 11
3333
4 : 100
3434
5 : 101
35-
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
35+
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
3636
</pre>
3737

3838
<p><strong class="example">Example 2:</strong></p>

‎solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131

3232
<pre>
3333
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
34-
<strong>输出:</strong>13
34+
<strong>输出:</strong>13
3535
<strong>解释:</strong>所有的子数组和为 1, 3, 6, 10, 2, 5, 9, 3, 7, 4 。将它们升序排序后,我们得到新的数组 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 1 到 ri = 5 的和为 1 +たす 2 +たす 3 +たす 3 +たす 4 = 13 。
3636
</pre>
3737

‎solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tags:
3030

3131
<pre>
3232
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
33-
<strong>Output:</strong> 13
34-
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 +たす 2 +たす 3 +たす 3 +たす 4 = 13.
33+
<strong>Output:</strong> 13
34+
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 +たす 2 +たす 3 +たす 3 +たす 4 = 13.
3535
</pre>
3636

3737
<p><strong class="example">Example 2:</strong></p>

‎solution/3200-3299/3237.Alt and Tab Simulation/README_EN.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3237.Al
3636

3737
<ul>
3838
<li>Initial order: <code>[1,2,3]</code></li>
39-
<li>After the first query: <code>[<u><strong>3</strong></u>,2,1]</code></li>
40-
<li>After the second query: <code>[<u><strong>3</strong></u>,2,1]</code></li>
39+
<li>After the first query: <code>[<u><strong>3</strong></u>,1,2]</code></li>
40+
<li>After the second query: <code>[<u><strong>3</strong></u>,1,2]</code></li>
4141
<li>After the last query: <code>[<u><strong>2</strong></u>,3,1]</code></li>
4242
</ul>
4343
</div>

‎solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md‎

Lines changed: 180 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,208 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3243.Sh
8383

8484
<!-- solution:start -->
8585

86-
### 方法一
86+
### 方法一:BFS
87+
88+
我们先建立一个有向图 $\textit{g},ドル其中 $\textit{g}[i]$ 表示从城市 $i$ 出发可以到达的城市列表,初始时,每个城市 $i$ 都有一条单向道路通往城市 $i + 1$。
89+
90+
然后,我们对每个查询 $[u, v],ドル将 $u$ 添加到 $v$ 的出发城市列表中,然后使用 BFS 求出从城市 0ドル$ 到城市 $n - 1$ 的最短路径长度,将结果添加到答案数组中。
91+
92+
最后返回答案数组即可。
93+
94+
时间复杂度 $O(q \times (n + q)),ドル空间复杂度 $O(n + q)$。其中 $n$ 和 $q$ 分别为城市数量和查询数量。
8795

8896
<!-- tabs:start -->
8997

9098
#### Python3
9199

92100
```python
93-
101+
class Solution:
102+
def shortestDistanceAfterQueries(
103+
self, n: int, queries: List[List[int]]
104+
) -> List[int]:
105+
def bfs(i: int) -> int:
106+
q = deque([i])
107+
vis = [False] * n
108+
vis[i] = True
109+
d = 0
110+
while 1:
111+
for _ in range(len(q)):
112+
u = q.popleft()
113+
if u == n - 1:
114+
return d
115+
for v in g[u]:
116+
if not vis[v]:
117+
vis[v] = True
118+
q.append(v)
119+
d += 1
120+
121+
g = [[i + 1] for i in range(n - 1)]
122+
ans = []
123+
for u, v in queries:
124+
g[u].append(v)
125+
ans.append(bfs(0))
126+
return ans
94127
```
95128

96129
#### Java
97130

98131
```java
99-
132+
class Solution {
133+
private List<Integer>[] g;
134+
private int n;
135+
136+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
137+
this.n = n;
138+
g = new List[n];
139+
Arrays.setAll(g, i -> new ArrayList<>());
140+
for (int i = 0; i < n - 1; ++i) {
141+
g[i].add(i + 1);
142+
}
143+
int m = queries.length;
144+
int[] ans = new int[m];
145+
for (int i = 0; i < m; ++i) {
146+
int u = queries[i][0], v = queries[i][1];
147+
g[u].add(v);
148+
ans[i] = bfs(0);
149+
}
150+
return ans;
151+
}
152+
153+
private int bfs(int i) {
154+
Deque<Integer> q = new ArrayDeque<>();
155+
q.offer(i);
156+
boolean[] vis = new boolean[n];
157+
vis[i] = true;
158+
for (int d = 0;; ++d) {
159+
for (int k = q.size(); k > 0; --k) {
160+
int u = q.poll();
161+
if (u == n - 1) {
162+
return d;
163+
}
164+
for (int v : g[u]) {
165+
if (!vis[v]) {
166+
vis[v] = true;
167+
q.offer(v);
168+
}
169+
}
170+
}
171+
}
172+
}
173+
}
100174
```
101175

102176
#### C++
103177

104178
```cpp
105-
179+
class Solution {
180+
public:
181+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
182+
vector<int> g[n];
183+
for (int i = 0; i < n - 1; ++i) {
184+
g[i].push_back(i + 1);
185+
}
186+
auto bfs = [&](int i) -> int {
187+
queue<int> q{{i}};
188+
vector<bool> vis(n);
189+
vis[i] = true;
190+
for (int d = 0;; ++d) {
191+
for (int k = q.size(); k; --k) {
192+
int u = q.front();
193+
q.pop();
194+
if (u == n - 1) {
195+
return d;
196+
}
197+
for (int v : g[u]) {
198+
if (!vis[v]) {
199+
vis[v] = true;
200+
q.push(v);
201+
}
202+
}
203+
}
204+
}
205+
};
206+
vector<int> ans;
207+
for (const auto& q : queries) {
208+
g[q[0]].push_back(q[1]);
209+
ans.push_back(bfs(0));
210+
}
211+
return ans;
212+
}
213+
};
106214
```
107215
108216
#### Go
109217
110218
```go
219+
func shortestDistanceAfterQueries(n int, queries [][]int) []int {
220+
g := make([][]int, n)
221+
for i := range g {
222+
g[i] = append(g[i], i+1)
223+
}
224+
bfs := func(i int) int {
225+
q := []int{i}
226+
vis := make([]bool, n)
227+
vis[i] = true
228+
for d := 0; ; d++ {
229+
for k := len(q); k > 0; k-- {
230+
u := q[0]
231+
if u == n-1 {
232+
return d
233+
}
234+
q = q[1:]
235+
for _, v := range g[u] {
236+
if !vis[v] {
237+
vis[v] = true
238+
q = append(q, v)
239+
}
240+
}
241+
}
242+
}
243+
}
244+
ans := make([]int, len(queries))
245+
for i, q := range queries {
246+
g[q[0]] = append(g[q[0]], q[1])
247+
ans[i] = bfs(0)
248+
}
249+
return ans
250+
}
251+
```
111252

253+
#### TypeScript
254+
255+
```ts
256+
function shortestDistanceAfterQueries(n: number, queries: number[][]): number[] {
257+
const g: number[][] = Array.from({ length: n }, () => []);
258+
for (let i = 0; i < n - 1; ++i) {
259+
g[i].push(i + 1);
260+
}
261+
const bfs = (i: number): number => {
262+
const q: number[] = [i];
263+
const vis: boolean[] = Array(n).fill(false);
264+
vis[i] = true;
265+
for (let d = 0; ; ++d) {
266+
const nq: number[] = [];
267+
for (const u of q) {
268+
if (u === n - 1) {
269+
return d;
270+
}
271+
for (const v of g[u]) {
272+
if (!vis[v]) {
273+
vis[v] = true;
274+
nq.push(v);
275+
}
276+
}
277+
}
278+
q.splice(0, q.length, ...nq);
279+
}
280+
};
281+
const ans: number[] = [];
282+
for (const [u, v] of queries) {
283+
g[u].push(v);
284+
ans.push(bfs(0));
285+
}
286+
return ans;
287+
}
112288
```
113289

114290
<!-- tabs:end -->

0 commit comments

Comments
(0)

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