Skip to content

Commit 2868b7d

Browse files
authored
feat: add solutions to lc problem: No.3532 (#4388)
No.3532.Path Existence Queries in a Graph I
1 parent c75bdf1 commit 2868b7d

File tree

7 files changed

+280
-8
lines changed

7 files changed

+280
-8
lines changed

solution/3500-3599/3532.Path Existence Queries in a Graph I/README.md

+97-4
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,125 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3532.Pa
8484

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

87-
### 方法一
87+
### 方法一:分组
88+
89+
根据题目描述,同一个连通分量的节点编号,一定是连续的。因此,我们可以用一个数组 $g$ 来记录每个节点所在的连通分量编号,用一个变量 $\textit{cnt}$ 来记录当前连通分量的编号。遍历 $\textit{nums}$ 数组,如果当前节点和前一个节点的差值大于 $\textit{maxDiff}$,则说明当前节点和前一个节点不在同一个连通分量中,我们就将 $\textit{cnt}$ 加 1。然后,我们将当前节点的连通分量编号赋值为 $\textit{cnt}$。
90+
91+
最后,对于每个查询 $(u, v)$,我们只需要判断 $g[u]$ 和 $g[v]$ 是否相等即可,如果相等,则说明 $u$ 和 $v$ 在同一个连通分量中,那么第 $i$ 个查询的答案就是 $\text{true}$,否则就是 $\text{false}$。
92+
93+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 $\textit{nums}$ 数组的长度。
8894

8995
<!-- tabs:start -->
9096

9197
#### Python3
9298

9399
```python
94-
100+
class Solution:
101+
def pathExistenceQueries(
102+
self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]
103+
) -> List[bool]:
104+
g = [0] * n
105+
cnt = 0
106+
for i in range(1, n):
107+
if nums[i] - nums[i - 1] > maxDiff:
108+
cnt += 1
109+
g[i] = cnt
110+
return [g[u] == g[v] for u, v in queries]
95111
```
96112

97113
#### Java
98114

99115
```java
100-
116+
class Solution {
117+
public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) {
118+
int[] g = new int[n];
119+
int cnt = 0;
120+
for (int i = 1; i < n; ++i) {
121+
if (nums[i] - nums[i - 1] > maxDiff) {
122+
cnt++;
123+
}
124+
g[i] = cnt;
125+
}
126+
127+
int m = queries.length;
128+
boolean[] ans = new boolean[m];
129+
for (int i = 0; i < m; ++i) {
130+
int u = queries[i][0];
131+
int v = queries[i][1];
132+
ans[i] = g[u] == g[v];
133+
}
134+
return ans;
135+
}
136+
}
101137
```
102138

103139
#### C++
104140

105141
```cpp
106-
142+
class Solution {
143+
public:
144+
vector<bool> pathExistenceQueries(int n, vector<int>& nums, int maxDiff, vector<vector<int>>& queries) {
145+
vector<int> g(n);
146+
int cnt = 0;
147+
for (int i = 1; i < n; ++i) {
148+
if (nums[i] - nums[i - 1] > maxDiff) {
149+
++cnt;
150+
}
151+
g[i] = cnt;
152+
}
153+
154+
vector<bool> ans;
155+
for (const auto& q : queries) {
156+
int u = q[0], v = q[1];
157+
ans.push_back(g[u] == g[v]);
158+
}
159+
return ans;
160+
}
161+
};
107162
```
108163

109164
#### Go
110165

111166
```go
167+
func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) {
168+
g := make([]int, n)
169+
cnt := 0
170+
for i := 1; i < n; i++ {
171+
if nums[i]-nums[i-1] > maxDiff {
172+
cnt++
173+
}
174+
g[i] = cnt
175+
}
176+
177+
for _, q := range queries {
178+
u, v := q[0], q[1]
179+
ans = append(ans, g[u] == g[v])
180+
}
181+
return
182+
}
183+
```
112184

185+
#### TypeScript
186+
187+
```ts
188+
function pathExistenceQueries(
189+
n: number,
190+
nums: number[],
191+
maxDiff: number,
192+
queries: number[][],
193+
): boolean[] {
194+
const g: number[] = Array(n).fill(0);
195+
let cnt = 0;
196+
197+
for (let i = 1; i < n; ++i) {
198+
if (nums[i] - nums[i - 1] > maxDiff) {
199+
++cnt;
200+
}
201+
g[i] = cnt;
202+
}
203+
204+
return queries.map(([u, v]) => g[u] === g[v]);
205+
}
113206
```
114207

115208
<!-- tabs:end -->

solution/3500-3599/3532.Path Existence Queries in a Graph I/README_EN.md

+97-4
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,125 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3532.Pa
8282

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

85-
### Solution 1
85+
### Solution 1: Grouping
86+
87+
According to the problem description, the node indices within the same connected component must be consecutive. Therefore, we can use an array $g$ to record the connected component index for each node and a variable $\textit{cnt}$ to track the current connected component index. As we iterate through the $\textit{nums}$ array, if the difference between the current node and the previous node is greater than $\textit{maxDiff}$, it indicates that the current node and the previous node are not in the same connected component. In this case, we increment $\textit{cnt}$. Then, we assign the current node's connected component index to $\textit{cnt}$.
88+
89+
Finally, for each query $(u, v)$, we only need to check whether $g[u]$ and $g[v]$ are equal. If they are equal, it means $u$ and $v$ are in the same connected component, and the answer for the $i$-th query is $\text{true}$. Otherwise, the answer is $\text{false}$.
90+
91+
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $\textit{nums}$ array.
8692

8793
<!-- tabs:start -->
8894

8995
#### Python3
9096

9197
```python
92-
98+
class Solution:
99+
def pathExistenceQueries(
100+
self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]
101+
) -> List[bool]:
102+
g = [0] * n
103+
cnt = 0
104+
for i in range(1, n):
105+
if nums[i] - nums[i - 1] > maxDiff:
106+
cnt += 1
107+
g[i] = cnt
108+
return [g[u] == g[v] for u, v in queries]
93109
```
94110

95111
#### Java
96112

97113
```java
98-
114+
class Solution {
115+
public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) {
116+
int[] g = new int[n];
117+
int cnt = 0;
118+
for (int i = 1; i < n; ++i) {
119+
if (nums[i] - nums[i - 1] > maxDiff) {
120+
cnt++;
121+
}
122+
g[i] = cnt;
123+
}
124+
125+
int m = queries.length;
126+
boolean[] ans = new boolean[m];
127+
for (int i = 0; i < m; ++i) {
128+
int u = queries[i][0];
129+
int v = queries[i][1];
130+
ans[i] = g[u] == g[v];
131+
}
132+
return ans;
133+
}
134+
}
99135
```
100136

101137
#### C++
102138

103139
```cpp
104-
140+
class Solution {
141+
public:
142+
vector<bool> pathExistenceQueries(int n, vector<int>& nums, int maxDiff, vector<vector<int>>& queries) {
143+
vector<int> g(n);
144+
int cnt = 0;
145+
for (int i = 1; i < n; ++i) {
146+
if (nums[i] - nums[i - 1] > maxDiff) {
147+
++cnt;
148+
}
149+
g[i] = cnt;
150+
}
151+
152+
vector<bool> ans;
153+
for (const auto& q : queries) {
154+
int u = q[0], v = q[1];
155+
ans.push_back(g[u] == g[v]);
156+
}
157+
return ans;
158+
}
159+
};
105160
```
106161

107162
#### Go
108163

109164
```go
165+
func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) {
166+
g := make([]int, n)
167+
cnt := 0
168+
for i := 1; i < n; i++ {
169+
if nums[i]-nums[i-1] > maxDiff {
170+
cnt++
171+
}
172+
g[i] = cnt
173+
}
174+
175+
for _, q := range queries {
176+
u, v := q[0], q[1]
177+
ans = append(ans, g[u] == g[v])
178+
}
179+
return
180+
}
181+
```
110182

183+
#### TypeScript
184+
185+
```ts
186+
function pathExistenceQueries(
187+
n: number,
188+
nums: number[],
189+
maxDiff: number,
190+
queries: number[][],
191+
): boolean[] {
192+
const g: number[] = Array(n).fill(0);
193+
let cnt = 0;
194+
195+
for (let i = 1; i < n; ++i) {
196+
if (nums[i] - nums[i - 1] > maxDiff) {
197+
++cnt;
198+
}
199+
g[i] = cnt;
200+
}
201+
202+
return queries.map(([u, v]) => g[u] === g[v]);
203+
}
111204
```
112205

113206
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
vector<bool> pathExistenceQueries(int n, vector<int>& nums, int maxDiff, vector<vector<int>>& queries) {
4+
vector<int> g(n);
5+
int cnt = 0;
6+
for (int i = 1; i < n; ++i) {
7+
if (nums[i] - nums[i - 1] > maxDiff) {
8+
++cnt;
9+
}
10+
g[i] = cnt;
11+
}
12+
13+
vector<bool> ans;
14+
for (const auto& q : queries) {
15+
int u = q[0], v = q[1];
16+
ans.push_back(g[u] == g[v]);
17+
}
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) {
2+
g := make([]int, n)
3+
cnt := 0
4+
for i := 1; i < n; i++ {
5+
if nums[i]-nums[i-1] > maxDiff {
6+
cnt++
7+
}
8+
g[i] = cnt
9+
}
10+
11+
for _, q := range queries {
12+
u, v := q[0], q[1]
13+
ans = append(ans, g[u] == g[v])
14+
}
15+
return
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) {
3+
int[] g = new int[n];
4+
int cnt = 0;
5+
for (int i = 1; i < n; ++i) {
6+
if (nums[i] - nums[i - 1] > maxDiff) {
7+
cnt++;
8+
}
9+
g[i] = cnt;
10+
}
11+
12+
int m = queries.length;
13+
boolean[] ans = new boolean[m];
14+
for (int i = 0; i < m; ++i) {
15+
int u = queries[i][0];
16+
int v = queries[i][1];
17+
ans[i] = g[u] == g[v];
18+
}
19+
return ans;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def pathExistenceQueries(
3+
self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]]
4+
) -> List[bool]:
5+
g = [0] * n
6+
cnt = 0
7+
for i in range(1, n):
8+
if nums[i] - nums[i - 1] > maxDiff:
9+
cnt += 1
10+
g[i] = cnt
11+
return [g[u] == g[v] for u, v in queries]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function pathExistenceQueries(
2+
n: number,
3+
nums: number[],
4+
maxDiff: number,
5+
queries: number[][],
6+
): boolean[] {
7+
const g: number[] = Array(n).fill(0);
8+
let cnt = 0;
9+
10+
for (let i = 1; i < n; ++i) {
11+
if (nums[i] - nums[i - 1] > maxDiff) {
12+
++cnt;
13+
}
14+
g[i] = cnt;
15+
}
16+
17+
return queries.map(([u, v]) => g[u] === g[v]);
18+
}

0 commit comments

Comments
 (0)