Tap here to switch tabs
Problem
Submissions

Problem: Path Existence Queries in a Graph II

med
30 min
Try to solve the Path Existence Queries in a Graph II problem.

Statement

You are given an integer n, an integer array nums of length n, an integer maxDiff, and a 22D integer array queries.

An undirected graph with n nodes labeled from 00 to n - 1 is constructed such that an edge exists between node i and node j if and only if ∣| nums[i] −- nums[j] ∣≤| \leq maxDiff.

For each query queries[i] == [u_i, v_i], determine the minimum distance (the minimum number of edges on a path) between node u_i and node v_i. If there is no path between the two nodes, the answer for that query is −1-1.

Return an array of integers representing the answers to all queries.

Constraints:

  • 1≤1 \leq n ==== nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤105\leq 10^5

  • 0≤0 \leq maxDiff ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i] ==== [u_i, v_i]

  • 0≤0 \leq u_i, v_i <n< n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Path Existence Queries in a Graph II

med
30 min
Try to solve the Path Existence Queries in a Graph II problem.

Statement

You are given an integer n, an integer array nums of length n, an integer maxDiff, and a 22D integer array queries.

An undirected graph with n nodes labeled from 00 to n - 1 is constructed such that an edge exists between node i and node j if and only if ∣| nums[i] −- nums[j] ∣≤| \leq maxDiff.

For each query queries[i] == [u_i, v_i], determine the minimum distance (the minimum number of edges on a path) between node u_i and node v_i. If there is no path between the two nodes, the answer for that query is −1-1.

Return an array of integers representing the answers to all queries.

Constraints:

  • 1≤1 \leq n ==== nums.length ≤105\leq 10^5

  • 0≤0 \leq nums[i] ≤105\leq 10^5

  • 0≤0 \leq maxDiff ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • queries[i] ==== [u_i, v_i]

  • 0≤0 \leq u_i, v_i <n< n