Sponsored
Sponsored
We can use a union-find (disjoint-set) data structure to solve the problem efficiently. The key idea is to process nodes in order of their values and use the union-find structure to keep track of connected components where nodes can potentially form good paths.
Steps:
Time Complexity: O(E log V + V log V), where E is the number of edges and V is the number of vertices, due to sorting and union-find operations.
Space Complexity: O(V), for storing parent ranks and counts in the union-find structure.
1class UnionFind:
2
3 def __init__(self, n):
4 self.parent = list(range(n))
5 self.rank = [1] * n
6 self.count = [1] * n
7
8 def find(self, x):
9 if self.parent[x] != x:
10 self.parent[x] = self.find(self.parent[x])
11 return self.parent[x]
12
13 def union(self, x, y):
14 rootX = self.find(x)
15 rootY = self.find(y)
16 if rootX != rootY:
17 if self.rank[rootX] > self.rank[rootY]:
18 self.parent[rootY] = rootX
19 self.count[rootX] += self.count[rootY]
20 elif self.rank[rootX] < self.rank[rootY]:
21 self.parent[rootX] = rootY
22 self.count[rootY] += self.count[rootX]
23 else:
24 self.parent[rootY] = rootX
25 self.count[rootX] += self.count[rootY]
26 self.rank[rootX] += 1
27 return True
28 return False
29
30 def size(self, x):
31 return self.count[self.find(x)]
32
33 def numberOfGoodPaths(vals, edges):
34 n = len(vals)
35 uf = UnionFind(n)
36 neighbors = [[] for _ in range(n)]
37
38 for u, v in edges:
39 neighbors[u].append(v)
40 neighbors[v].append(u)
41
42 value_to_nodes = sorted(range(n), key=lambda x: vals[x])
43 result = n
44
45 for i in value_to_nodes:
46 current_value = vals[i]
47
48 for neighbor in neighbors[i]:
49 if vals[neighbor] <= current_value:
50 uf.union(i, neighbor)
51
52 component_size = 0
53 for neighbor in neighbors[i]:
54 if vals[neighbor] == current_value:
55 if uf.find(neighbor) == uf.find(i):
56 component_size += uf.size(neighbor)
57 result += component_size // 2
58
59 return result
The solution initializes a union-find structure to track connected nodes. Nodes are sorted by their values so that processing happens in a value-based order. The union-find data structure helps recognize connected components efficiently and handle their size calculations to find the number of good paths effectively.
Another way to solve the problem is by using Depth-First Search (DFS) to explore available paths and check if they satisfy the good path conditions. This approach typically involves building the graph from edges and recursively checking paths using DFS while keeping track of the path max value.
Steps:
This approach can become computationally expensive for large graphs but serves as a brute force method to ensure all paths are checked.
Time Complexity: O(V + E) per path exploration, but can be exponential (potentially O(V^2)) if exploring all node pairs for paths.
Space Complexity: O(V), as it uses recursion stack and storage for unique paths.
1import java.util.
In this solution, the adjacency list representation allows for easy traversal of nodes. The DFS method visits nodes to form paths and checks conditions for being a good path when visiting from node to node.