Class Solution
-
- All Implemented Interfaces:
public final class Solution3532 - Path Existence Queries in a Graph I.
Medium
You are given an integer
nrepresenting the number of nodes in a graph, labeled from 0 ton - 1.You are also given an integer array
numsof lengthnsorted in non-decreasing order, and an integermaxDiff.An undirected edge exists between nodes
iandjif the absolute difference betweennums[i]andnums[j]is at mostmaxDiff(i.e.,|nums[i] - nums[j]| <= maxDiff).You are also given a 2D integer array
queries. For each <code>queriesi = u<sub>i</sub>, v<sub>i</sub></code>, determine whether there exists a path between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.Return a boolean array
answer, whereanswer[i]istrueif there exists a path between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the <code>i<sup>th</sup></code> query andfalseotherwise.Example 1:
Input: n = 2, nums = 1,3, maxDiff = 1, queries = [0,0,0,1]
Output: true,false
Explanation:
Query
[0,0]: Node 0 has a trivial path to itself.Query
[0,1]: There is no edge between Node 0 and Node 1 because|nums[0] - nums[1]| = |1 - 3| = 2, which is greater thanmaxDiff.Thus, the final answer after processing all the queries is
[true, false].
Example 2:
Input: n = 4, nums = 2,5,6,8, maxDiff = 2, queries = [0,1,0,2,1,3,2,3]
Output: false,false,true,true
Explanation:
The resulting graph is:
Query
[0,1]: There is no edge between Node 0 and Node 1 because|nums[0] - nums[1]| = |2 - 5| = 3, which is greater thanmaxDiff.Query
[0,2]: There is no edge between Node 0 and Node 2 because|nums[0] - nums[2]| = |2 - 6| = 4, which is greater thanmaxDiff.Query
[1,3]: There is a path between Node 1 and Node 3 through Node 2 since|nums[1] - nums[2]| = |5 - 6| = 1and|nums[2] - nums[3]| = |6 - 8| = 2, both of which are withinmaxDiff.Query
[2,3]: There is an edge between Node 2 and Node 3 because|nums[2] - nums[3]| = |6 - 8| = 2, which is equal tomaxDiff.Thus, the final answer after processing all the queries is
[false, false, true, true].
Constraints:
<code>1 <= n == nums.length <= 10<sup>5</sup></code>
<code>0 <= numsi<= 10<sup>5</sup></code>
numsis sorted in non-decreasing order.<code>0 <= maxDiff <= 10<sup>5</sup></code>
<code>1 <= queries.length <= 10<sup>5</sup></code>
<code>queriesi == u<sub>i</sub>, v<sub>i</sub></code>
<code>0 <= u<sub>i</sub>, v<sub>i</sub>< n</code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final BooleanArraypathExistenceQueries(Integer n, IntArray nums, Integer maxDiff, Array<IntArray> queries)-
-
Method Detail
-
pathExistenceQueries
final BooleanArray pathExistenceQueries(Integer n, IntArray nums, Integer maxDiff, Array<IntArray> queries)
-
-
-
-