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 e93c45d

Browse files
feat: add solutions to lc problem: No.3244 (doocs#3364)
No.3244.Shortest Distance After Road Addition Queries II
1 parent f524a9a commit e93c45d

File tree

7 files changed

+335
-8
lines changed

7 files changed

+335
-8
lines changed

‎solution/3200-3299/3244.Shortest Distance After Road Addition Queries II/README.md‎

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,144 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3244.Sh
8686

8787
<!-- solution:start -->
8888

89-
### 方法一
89+
### 方法一:贪心 + 记录跳转位置
90+
91+
我们定义一个长度为 $n - 1$ 的数组 $\textit{nxt},ドル其中 $\textit{nxt}[i]$ 表示从城市 $i$ 可以到达的下一个城市的编号。初始时 $\textit{nxt}[i] = i + 1$。
92+
93+
对于每次查询 $[u, v],ドル如果此前已经连通了 $u'$ 和 $v',ドル且 $u' <= u < v <= v',ドル那么我们可以跳过这次查询。否则,我们需要将 $nxt[u]$ 到 $nxt[v - 1]$ 这些城市的下一个城市编号设置为 0ドル,ドル并将 $nxt[u]$ 设置为 $v$。
94+
95+
在这个过程中,我们维护一个变量 $\textit{cnt},ドル表示从城市 0ドル$ 到城市 $n - 1$ 的最短路径的长度。初始时 $\textit{cnt} = n - 1$。每一次,如果我们将 $[\textit{nxt}[u], \textit{v})$ 这些城市的下一个城市编号设置为 0ドル,ドル那么 $\textit{cnt}$ 就会减少 1ドル$。
96+
97+
时间复杂度 $O(n + q),ドル空间复杂度 $O(n)$。其中 $n$ 和 $q$ 分别是城市数量和查询数量。
9098

9199
<!-- tabs:start -->
92100

93101
#### Python3
94102

95103
```python
96-
104+
class Solution:
105+
def shortestDistanceAfterQueries(
106+
self, n: int, queries: List[List[int]]
107+
) -> List[int]:
108+
nxt = list(range(1, n))
109+
ans = []
110+
cnt = n - 1
111+
for u, v in queries:
112+
if 0 < nxt[u] < v:
113+
i = nxt[u]
114+
while i < v:
115+
cnt -= 1
116+
nxt[i], i = 0, nxt[i]
117+
nxt[u] = v
118+
ans.append(cnt)
119+
return ans
97120
```
98121

99122
#### Java
100123

101124
```java
102-
125+
class Solution {
126+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
127+
int[] nxt = new int[n - 1];
128+
for (int i = 1; i < n; ++i) {
129+
nxt[i - 1] = i;
130+
}
131+
int m = queries.length;
132+
int cnt = n - 1;
133+
int[] ans = new int[m];
134+
for (int i = 0; i < m; ++i) {
135+
int u = queries[i][0], v = queries[i][1];
136+
if (nxt[u] > 0 && nxt[u] < v) {
137+
int j = nxt[u];
138+
while (j < v) {
139+
--cnt;
140+
int t = nxt[j];
141+
nxt[j] = 0;
142+
j = t;
143+
}
144+
nxt[u] = v;
145+
}
146+
ans[i] = cnt;
147+
}
148+
return ans;
149+
}
150+
}
103151
```
104152

105153
#### C++
106154

107155
```cpp
108-
156+
class Solution {
157+
public:
158+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
159+
vector<int> nxt(n - 1);
160+
iota(nxt.begin(), nxt.end(), 1);
161+
int cnt = n - 1;
162+
vector<int> ans;
163+
for (const auto& q : queries) {
164+
int u = q[0], v = q[1];
165+
if (nxt[u] && nxt[u] < v) {
166+
int i = nxt[u];
167+
while (i < v) {
168+
--cnt;
169+
int t = nxt[i];
170+
nxt[i] = 0;
171+
i = t;
172+
}
173+
nxt[u] = v;
174+
}
175+
ans.push_back(cnt);
176+
}
177+
return ans;
178+
}
179+
};
109180
```
110181
111182
#### Go
112183
113184
```go
185+
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
186+
nxt := make([]int, n-1)
187+
for i := range nxt {
188+
nxt[i] = i + 1
189+
}
190+
cnt := n - 1
191+
for _, q := range queries {
192+
u, v := q[0], q[1]
193+
if nxt[u] > 0 && nxt[u] < v {
194+
i := nxt[u]
195+
for i < v {
196+
cnt--
197+
nxt[i], i = 0, nxt[i]
198+
}
199+
nxt[u] = v
200+
}
201+
ans = append(ans, cnt)
202+
}
203+
return
204+
}
205+
```
114206

207+
#### TypeScript
208+
209+
```ts
210+
function shortestDistanceAfterQueries(n: number, queries: number[][]): number[] {
211+
const nxt: number[] = Array.from({ length: n - 1 }, (_, i) => i + 1);
212+
const ans: number[] = [];
213+
let cnt = n - 1;
214+
for (const [u, v] of queries) {
215+
if (nxt[u] && nxt[u] < v) {
216+
let i = nxt[u];
217+
while (i < v) {
218+
--cnt;
219+
[nxt[i], i] = [0, nxt[i]];
220+
}
221+
nxt[u] = v;
222+
}
223+
ans.push(cnt);
224+
}
225+
return ans;
226+
}
115227
```
116228

117229
<!-- tabs:end -->

‎solution/3200-3299/3244.Shortest Distance After Road Addition Queries II/README_EN.md‎

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,144 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3244.Sh
8484

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

87-
### Solution 1
87+
### Solution 1: Greedy + Recording Jump Positions
88+
89+
We define an array $\textit{nxt}$ of length $n - 1,ドル where $\textit{nxt}[i]$ represents the next city that can be reached from city $i$. Initially, $\textit{nxt}[i] = i + 1$.
90+
91+
For each query $[u, v],ドル if $u'$ and $v'$ have already been connected before, and $u' \leq u < v \leq v',ドル then we can skip this query. Otherwise, we need to set the next city number for cities from $\textit{nxt}[u]$ to $\textit{nxt}[v - 1]$ to 0ドル,ドル and set $\textit{nxt}[u]$ to $v$.
92+
93+
During this process, we maintain a variable $\textit{cnt},ドル which represents the length of the shortest path from city 0ドル$ to city $n - 1$. Initially, $\textit{cnt} = n - 1$. Each time we set the next city number for cities in $[\textit{nxt}[u], \textit{v})$ to 0ドル,ドル $\textit{cnt}$ decreases by 1ドル$.
94+
95+
Time complexity is $O(n + q),ドル and space complexity is $O(n)$. Here, $n$ and $q$ are the number of cities and the number of queries, respectively.
8896

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

9199
#### Python3
92100

93101
```python
94-
102+
class Solution:
103+
def shortestDistanceAfterQueries(
104+
self, n: int, queries: List[List[int]]
105+
) -> List[int]:
106+
nxt = list(range(1, n))
107+
ans = []
108+
cnt = n - 1
109+
for u, v in queries:
110+
if 0 < nxt[u] < v:
111+
i = nxt[u]
112+
while i < v:
113+
cnt -= 1
114+
nxt[i], i = 0, nxt[i]
115+
nxt[u] = v
116+
ans.append(cnt)
117+
return ans
95118
```
96119

97120
#### Java
98121

99122
```java
100-
123+
class Solution {
124+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
125+
int[] nxt = new int[n - 1];
126+
for (int i = 1; i < n; ++i) {
127+
nxt[i - 1] = i;
128+
}
129+
int m = queries.length;
130+
int cnt = n - 1;
131+
int[] ans = new int[m];
132+
for (int i = 0; i < m; ++i) {
133+
int u = queries[i][0], v = queries[i][1];
134+
if (nxt[u] > 0 && nxt[u] < v) {
135+
int j = nxt[u];
136+
while (j < v) {
137+
--cnt;
138+
int t = nxt[j];
139+
nxt[j] = 0;
140+
j = t;
141+
}
142+
nxt[u] = v;
143+
}
144+
ans[i] = cnt;
145+
}
146+
return ans;
147+
}
148+
}
101149
```
102150

103151
#### C++
104152

105153
```cpp
106-
154+
class Solution {
155+
public:
156+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
157+
vector<int> nxt(n - 1);
158+
iota(nxt.begin(), nxt.end(), 1);
159+
int cnt = n - 1;
160+
vector<int> ans;
161+
for (const auto& q : queries) {
162+
int u = q[0], v = q[1];
163+
if (nxt[u] && nxt[u] < v) {
164+
int i = nxt[u];
165+
while (i < v) {
166+
--cnt;
167+
int t = nxt[i];
168+
nxt[i] = 0;
169+
i = t;
170+
}
171+
nxt[u] = v;
172+
}
173+
ans.push_back(cnt);
174+
}
175+
return ans;
176+
}
177+
};
107178
```
108179
109180
#### Go
110181
111182
```go
183+
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
184+
nxt := make([]int, n-1)
185+
for i := range nxt {
186+
nxt[i] = i + 1
187+
}
188+
cnt := n - 1
189+
for _, q := range queries {
190+
u, v := q[0], q[1]
191+
if nxt[u] > 0 && nxt[u] < v {
192+
i := nxt[u]
193+
for i < v {
194+
cnt--
195+
nxt[i], i = 0, nxt[i]
196+
}
197+
nxt[u] = v
198+
}
199+
ans = append(ans, cnt)
200+
}
201+
return
202+
}
203+
```
112204

205+
#### TypeScript
206+
207+
```ts
208+
function shortestDistanceAfterQueries(n: number, queries: number[][]): number[] {
209+
const nxt: number[] = Array.from({ length: n - 1 }, (_, i) => i + 1);
210+
const ans: number[] = [];
211+
let cnt = n - 1;
212+
for (const [u, v] of queries) {
213+
if (nxt[u] && nxt[u] < v) {
214+
let i = nxt[u];
215+
while (i < v) {
216+
--cnt;
217+
[nxt[i], i] = [0, nxt[i]];
218+
}
219+
nxt[u] = v;
220+
}
221+
ans.push(cnt);
222+
}
223+
return ans;
224+
}
113225
```
114226

115227
<!-- tabs:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
4+
vector<int> nxt(n - 1);
5+
iota(nxt.begin(), nxt.end(), 1);
6+
int cnt = n - 1;
7+
vector<int> ans;
8+
for (const auto& q : queries) {
9+
int u = q[0], v = q[1];
10+
if (nxt[u] && nxt[u] < v) {
11+
int i = nxt[u];
12+
while (i < v) {
13+
--cnt;
14+
int t = nxt[i];
15+
nxt[i] = 0;
16+
i = t;
17+
}
18+
nxt[u] = v;
19+
}
20+
ans.push_back(cnt);
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func shortestDistanceAfterQueries(n int, queries [][]int) (ans []int) {
2+
nxt := make([]int, n-1)
3+
for i := range nxt {
4+
nxt[i] = i + 1
5+
}
6+
cnt := n - 1
7+
for _, q := range queries {
8+
u, v := q[0], q[1]
9+
if nxt[u] > 0 && nxt[u] < v {
10+
i := nxt[u]
11+
for i < v {
12+
cnt--
13+
nxt[i], i = 0, nxt[i]
14+
}
15+
nxt[u] = v
16+
}
17+
ans = append(ans, cnt)
18+
}
19+
return
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
3+
int[] nxt = new int[n - 1];
4+
for (int i = 1; i < n; ++i) {
5+
nxt[i - 1] = i;
6+
}
7+
int m = queries.length;
8+
int cnt = n - 1;
9+
int[] ans = new int[m];
10+
for (int i = 0; i < m; ++i) {
11+
int u = queries[i][0], v = queries[i][1];
12+
if (nxt[u] > 0 && nxt[u] < v) {
13+
int j = nxt[u];
14+
while (j < v) {
15+
--cnt;
16+
int t = nxt[j];
17+
nxt[j] = 0;
18+
j = t;
19+
}
20+
nxt[u] = v;
21+
}
22+
ans[i] = cnt;
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def shortestDistanceAfterQueries(
3+
self, n: int, queries: List[List[int]]
4+
) -> List[int]:
5+
nxt = list(range(1, n))
6+
ans = []
7+
cnt = n - 1
8+
for u, v in queries:
9+
if 0 < nxt[u] < v:
10+
i = nxt[u]
11+
while i < v:
12+
cnt -= 1
13+
nxt[i], i = 0, nxt[i]
14+
nxt[u] = v
15+
ans.append(cnt)
16+
return ans

0 commit comments

Comments
(0)

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