diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/README.md b/solution/3500-3599/3532.Path Existence Queries in a Graph I/README.md index 1ce54d7c19e70..9f5551b4d7024 100644 --- a/solution/3500-3599/3532.Path Existence Queries in a Graph I/README.md +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/README.md @@ -84,32 +84,125 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3532.Pa -### 方法一 +### 方法一:分组 + +根据题目描述,同一个连通分量的节点编号,一定是连续的。因此,我们可以用一个数组 $g$ 来记录每个节点所在的连通分量编号,用一个变量 $\textit{cnt}$ 来记录当前连通分量的编号。遍历 $\textit{nums}$ 数组,如果当前节点和前一个节点的差值大于 $\textit{maxDiff}$,则说明当前节点和前一个节点不在同一个连通分量中,我们就将 $\textit{cnt}$ 加 1。然后,我们将当前节点的连通分量编号赋值为 $\textit{cnt}$。 + +最后,对于每个查询 $(u, v)$,我们只需要判断 $g[u]$ 和 $g[v]$ 是否相等即可,如果相等,则说明 $u$ 和 $v$ 在同一个连通分量中,那么第 $i$ 个查询的答案就是 $\text{true}$,否则就是 $\text{false}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是 $\textit{nums}$ 数组的长度。 #### Python3 ```python - +class Solution: + def pathExistenceQueries( + self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]] + ) -> List[bool]: + g = [0] * n + cnt = 0 + for i in range(1, n): + if nums[i] - nums[i - 1] > maxDiff: + cnt += 1 + g[i] = cnt + return [g[u] == g[v] for u, v in queries] ``` #### Java ```java - +class Solution { + public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) { + int[] g = new int[n]; + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + cnt++; + } + g[i] = cnt; + } + + int m = queries.length; + boolean[] ans = new boolean[m]; + for (int i = 0; i < m; ++i) { + int u = queries[i][0]; + int v = queries[i][1]; + ans[i] = g[u] == g[v]; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector pathExistenceQueries(int n, vector& nums, int maxDiff, vector>& queries) { + vector g(n); + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + vector ans; + for (const auto& q : queries) { + int u = q[0], v = q[1]; + ans.push_back(g[u] == g[v]); + } + return ans; + } +}; ``` #### Go ```go +func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) { + g := make([]int, n) + cnt := 0 + for i := 1; i < n; i++ { + if nums[i]-nums[i-1] > maxDiff { + cnt++ + } + g[i] = cnt + } + + for _, q := range queries { + u, v := q[0], q[1] + ans = append(ans, g[u] == g[v]) + } + return +} +``` +#### TypeScript + +```ts +function pathExistenceQueries( + n: number, + nums: number[], + maxDiff: number, + queries: number[][], +): boolean[] { + const g: number[] = Array(n).fill(0); + let cnt = 0; + + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + return queries.map(([u, v]) => g[u] === g[v]); +} ``` diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/README_EN.md b/solution/3500-3599/3532.Path Existence Queries in a Graph I/README_EN.md index bc3a58b484dc4..514f7fa65c401 100644 --- a/solution/3500-3599/3532.Path Existence Queries in a Graph I/README_EN.md +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/README_EN.md @@ -82,32 +82,125 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3532.Pa -### Solution 1 +### Solution 1: Grouping + +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}$. + +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}$. + +The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the $\textit{nums}$ array. #### Python3 ```python - +class Solution: + def pathExistenceQueries( + self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]] + ) -> List[bool]: + g = [0] * n + cnt = 0 + for i in range(1, n): + if nums[i] - nums[i - 1] > maxDiff: + cnt += 1 + g[i] = cnt + return [g[u] == g[v] for u, v in queries] ``` #### Java ```java - +class Solution { + public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) { + int[] g = new int[n]; + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + cnt++; + } + g[i] = cnt; + } + + int m = queries.length; + boolean[] ans = new boolean[m]; + for (int i = 0; i < m; ++i) { + int u = queries[i][0]; + int v = queries[i][1]; + ans[i] = g[u] == g[v]; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector pathExistenceQueries(int n, vector& nums, int maxDiff, vector>& queries) { + vector g(n); + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + vector ans; + for (const auto& q : queries) { + int u = q[0], v = q[1]; + ans.push_back(g[u] == g[v]); + } + return ans; + } +}; ``` #### Go ```go +func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) { + g := make([]int, n) + cnt := 0 + for i := 1; i < n; i++ { + if nums[i]-nums[i-1] > maxDiff { + cnt++ + } + g[i] = cnt + } + + for _, q := range queries { + u, v := q[0], q[1] + ans = append(ans, g[u] == g[v]) + } + return +} +``` +#### TypeScript + +```ts +function pathExistenceQueries( + n: number, + nums: number[], + maxDiff: number, + queries: number[][], +): boolean[] { + const g: number[] = Array(n).fill(0); + let cnt = 0; + + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + return queries.map(([u, v]) => g[u] === g[v]); +} ``` diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.cpp b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.cpp new file mode 100644 index 0000000000000..58d396a8b1051 --- /dev/null +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + vector pathExistenceQueries(int n, vector& nums, int maxDiff, vector>& queries) { + vector g(n); + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + vector ans; + for (const auto& q : queries) { + int u = q[0], v = q[1]; + ans.push_back(g[u] == g[v]); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.go b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.go new file mode 100644 index 0000000000000..fb88cf8325a93 --- /dev/null +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.go @@ -0,0 +1,16 @@ +func pathExistenceQueries(n int, nums []int, maxDiff int, queries [][]int) (ans []bool) { + g := make([]int, n) + cnt := 0 + for i := 1; i < n; i++ { + if nums[i]-nums[i-1] > maxDiff { + cnt++ + } + g[i] = cnt + } + + for _, q := range queries { + u, v := q[0], q[1] + ans = append(ans, g[u] == g[v]) + } + return +} diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.java b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.java new file mode 100644 index 0000000000000..8e5c7933cbfe4 --- /dev/null +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.java @@ -0,0 +1,21 @@ +class Solution { + public boolean[] pathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) { + int[] g = new int[n]; + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + cnt++; + } + g[i] = cnt; + } + + int m = queries.length; + boolean[] ans = new boolean[m]; + for (int i = 0; i < m; ++i) { + int u = queries[i][0]; + int v = queries[i][1]; + ans[i] = g[u] == g[v]; + } + return ans; + } +} diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.py b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.py new file mode 100644 index 0000000000000..66429dd2ba01f --- /dev/null +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.py @@ -0,0 +1,11 @@ +class Solution: + def pathExistenceQueries( + self, n: int, nums: List[int], maxDiff: int, queries: List[List[int]] + ) -> List[bool]: + g = [0] * n + cnt = 0 + for i in range(1, n): + if nums[i] - nums[i - 1] > maxDiff: + cnt += 1 + g[i] = cnt + return [g[u] == g[v] for u, v in queries] diff --git a/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.ts b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.ts new file mode 100644 index 0000000000000..bc749407e7ab4 --- /dev/null +++ b/solution/3500-3599/3532.Path Existence Queries in a Graph I/Solution.ts @@ -0,0 +1,18 @@ +function pathExistenceQueries( + n: number, + nums: number[], + maxDiff: number, + queries: number[][], +): boolean[] { + const g: number[] = Array(n).fill(0); + let cnt = 0; + + for (let i = 1; i < n; ++i) { + if (nums[i] - nums[i - 1] > maxDiff) { + ++cnt; + } + g[i] = cnt; + } + + return queries.map(([u, v]) => g[u] === g[v]); +}