LeetCode 1697: Checking Existence of Edge Length Limited Paths Solution
Master LeetCode problem 1697 (Checking Existence of Edge Length Limited Paths), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1697. Checking Existence of Edge Length Limited Paths
Problem Explanation
Explanation
To solve this problem, we can use the Union-Find (Disjoint Set Union) data structure. We will sort the edges by their distance in non-decreasing order. Then, for each query, starting from the shortest distance, we will merge the nodes connected by edges with distances less than the current query limit. After each merge operation, we can check if the nodes representing the query points are in the same connected component. If they are, then there exists a path with edge distances less than the query limit.
Algorithm:
- Sort the edges by their distance in non-decreasing order.
- Initialize the Union-Find data structure to keep track of connected components.
- For each query in queries:
- Start from the minimum distance edge and merge nodes until the current edge distance is less than the query limit.
- Check if the query points are in the same connected component after each merge operation.
Time Complexity:
- The time complexity of this approach is O((n + m) * log(m) + q * log(q)), where n is the number of nodes, m is the number of edges, and q is the number of queries.
Space Complexity:
- The space complexity is O(n) for the Union-Find data structure.
Solution Code
class Solution {
public List<Boolean> distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
Arrays.sort(edgeList, (a, b) -> Integer.compare(a[2], b[2]));
int q = queries.length;
List<Boolean> result = new ArrayList<>();
int[][] newQueries = new int[q][4];
for (int i = 0; i < q; i++) {
newQueries[i] = new int[]{queries[i][0], queries[i][1], queries[i][2], i};
}
Arrays.sort(newQueries, (a, b) -> Integer.compare(a[2], b[2]));
UnionFind uf = new UnionFind(n);
int idx = 0;
for (int i = 0; i < q; i++) {
while (idx < edgeList.length && edgeList[idx][2] < newQueries[i][2]) {
uf.union(edgeList[idx][0], edgeList[idx][1]);
idx++;
}
result.add(uf.find(newQueries[i][0]) == uf.find(newQueries[i][1]));
}
return result;
}
class UnionFind {
int[] parent;
public UnionFind(int n) {
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public void union(int a, int b) {
parent[find(a)] = find(b);
}
public int find(int a) {
if (parent[a] != a) {
parent[a] = find(parent[a]);
}
return parent[a];
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1697 (Checking Existence of Edge Length Limited Paths)?
This page provides optimized solutions for LeetCode problem 1697 (Checking Existence of Edge Length Limited Paths) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1697 (Checking Existence of Edge Length Limited Paths)?
The time complexity for LeetCode 1697 (Checking Existence of Edge Length Limited Paths) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1697 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1697 in Java, C++, or Python.