Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
Constraints:
n == vals.length
1 <= n <= 3 * 104
0 <= vals[i] <= 105
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
ai != bi
edges represents a valid tree.
Example 1:
Input: vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
Output: 6
Example 2:
Input: vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
Output: 7
Explanation: There are 5 good paths consisting of a single node.
There are 2 additional good paths: 0 -> 1 and 2 -> 3.
Example 3:
Input: vals = [1], edges = []
Output: 1
Explanation: The tree consists of only one node, so there is one good path.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Graph Problems, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
*General idea: use a union-find algorithm to group the vertices in the graph into connected components. Sort the edges based on the maximum value of the vertices in the edge to minimize the maximum value of the vertices in a good path. And return the number of good paths in the graph.
1. Create an adjacency list where adj[X] contains all the neighbors of node X.
2. Create a map valuesToNodes where valuesToNodes[X] is an array that contains all the nodes having the value X. The data structure chosen to create such a map sorts the keys in non-decreasing order, i.e., the keys are sorted.
3. Iterate over all the nodes and add each node to valuesToNodes[vals[node]].
4. Create a class UnionFind defining standard methods find and union_set.
5. Create an instance of UnionFind, passing the size as n. Also, initialize the count of good paths variable goodPaths with 0.
6. Iterate over each entry value, nodes in valuesToNodes in ascending order.
For every node in nodes, iterate over its neighbors.
For each neighbor of the node, if vals[node] >= vals[neighbor] we perform a union of the node with the neighbor.
After iterating through all the nodes, we create a map group. group[A] contains the number of nodes (from the nodes array) that belong to the same component A. For every node in nodes, we find its component and increment the size of that component by 1 in groups, i.e., group[find(node)] = group[find(node)] + 1.
We iterate through all the entries in the group and, for each key, get the value called size corresponding to it. Add (size * (size + 1) / 2) to the goodPaths.
7. Return goodPaths.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Union_Set:
def __init__(self,n):
self.parents = [i for i in range(n)]
self.rank = [0 for i in range(n)]
def find(self,x):
if self.parents[x] != x:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self,x,y):
xp, yp = self.find(x), self.find(y)
if xp == yp:
return
elif self.rank[xp] == self.rank[yp]:
self.parents[xp] = yp
self.rank[yp] += 1
elif self.rank[xp] < self.rank[yp]:
self.parents[xp] = yp
else:
self.parents[yp] = xp
class Solution:
def numberOfGoodPaths(self, vals: List[int], edges: List[List[int]]) -> int:
tree = defaultdict(lambda:set())
sorted_values = defaultdict(list)
keys = set()
for i in range(len(vals)):
keys.add(vals[i])
sorted_values[vals[i]].append(i)
for edge in edges:
tree[edge[0]].add(edge[1])
tree[edge[1]].add(edge[0])
skeys = sorted(list(keys))
components = Union_Set(len(vals))
res = 0
for k in skeys:
for node in sorted_values[k]:
for neighbour in tree[node]:
if vals[node] >= vals[neighbour]:
components.union(neighbour,node)
baby_graph = defaultdict(int)
for node in sorted_values[k]:
baby_graph[components.find(node)]+= 1
for baby,nums in baby_graph.items():
res += nums*(nums+1)/2
return int(res)
class UnionFind {
int[] parent;
int[] rank;
public UnionFind(int size) {
parent = new int[size];
for (int i = 0; i < size; i++)
parent[i] = i;
rank = new int[size];
}
public int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}
public void union_set(int x, int y) {
int xset = find(x), yset = find(y);
if (xset == yset) {
return;
} else if (rank[xset] < rank[yset]) {
parent[xset] = yset;
} else if (rank[xset] > rank[yset]) {
parent[yset] = xset;
} else {
parent[yset] = xset;
rank[xset]++;
}
}
}
class Solution {
public int numberOfGoodPaths(int[] vals, int[][] edges) {
Map<Integer, List<Integer>> adj = new HashMap<>();
for (int[] edge : edges) {
int a = edge[0], b = edge[1];
adj.computeIfAbsent(a, value -> new ArrayList<Integer>()).add(b);
adj.computeIfAbsent(b, value -> new ArrayList<Integer>()).add(a);
}
int n = vals.length;
// Mapping from value to all the nodes having the same value in sorted order of
// values.
TreeMap<Integer, List<Integer>> valuesToNodes = new TreeMap<>();
for (int i = 0; i < n; i++) {
valuesToNodes.computeIfAbsent(vals[i], value -> new ArrayList<Integer>()).add(i);
}
UnionFind dsu = new UnionFind(n);
int goodPaths = 0;
// Iterate over all the nodes with the same value in sorted order, starting from
// the lowest value.
for (int value : valuesToNodes.keySet()) {
// For every node in nodes, combine the sets of the node and its neighbors into
// one set.
for (int node : valuesToNodes.get(value)) {
if (!adj.containsKey(node))
continue;
for (int neighbor : adj.get(node)) {
// Only choose neighbors with a smaller value, as there is no point in
// traversing to other neighbors.
if (vals[node] >= vals[neighbor]) {
dsu.union_set(node, neighbor);
}
}
}
// Map to compute the number of nodes under observation (with the same values)
// in each of the sets.
Map<Integer, Integer> group = new HashMap<>();
// Iterate over all the nodes. Get the set of each node and increase the count
// of the set by 1.
for (int u : valuesToNodes.get(value)) {
group.put(dsu.find(u), group.getOrDefault(dsu.find(u), 0) + 1);
}
// For each set of "size", add size * (size + 1) / 2 to the number of goodPaths.
for (int key : group.keySet()) {
int size = group.get(key);
goodPaths += size * (size + 1) / 2;
}
}
return goodPaths;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Given n is the number of nodes,