Watch 10 video solutions for Node With Highest Edge Score, a medium level problem involving Hash Table, Graph. This walkthrough by Coding Decoded has 826 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge.
The graph is represented by a given 0-indexed integer array edges of length n, where edges[i] indicates that there is a directed edge from node i to node edges[i].
The edge score of a node i is defined as the sum of the labels of all the nodes that have an edge pointing to i.
Return the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index.
Example 1:
Input: edges = [1,0,0,0,0,7,7,5] Output: 7 Explanation: - The nodes 1, 2, 3 and 4 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 + 3 + 4 = 10. - The node 0 has an edge pointing to node 1. The edge score of node 1 is 0. - The node 7 has an edge pointing to node 5. The edge score of node 5 is 7. - The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11. Node 7 has the highest edge score so return 7.
Example 2:
Input: edges = [2,0,0,2] Output: 0 Explanation: - The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3. - The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3. Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.
Constraints:
n == edges.length2 <= n <= 1050 <= edges[i] < nedges[i] != iProblem Overview: You are given an array edges where index i has a directed edge to edges[i]. The edge score of a node is the sum of indices of nodes pointing to it. Your task is to compute the score for every node and return the node with the highest score (smallest index if there is a tie).
Approach 1: Brute Force Score Calculation (O(n²) time, O(1) space)
The straightforward idea is to compute the score of each node by scanning the entire edges array and summing all indices i where edges[i] == node. This means for every node j, you iterate through all n elements to check incoming edges. While the implementation is simple and uses constant extra space, it performs redundant scans and becomes inefficient for large inputs. This approach mainly helps you reason about how scores are defined before optimizing the solution.
Approach 2: Using an Array to Keep Score (O(n) time, O(n) space)
A more efficient approach accumulates scores in a dedicated array. Create a score array of size n, initialized to zero. Iterate once through the edges array. For each index i, add i to score[edges[i]]. This directly builds the total edge score for every node in a single pass. After filling the score array, scan it once more to find the node with the maximum score, resolving ties by selecting the smallest index. This method runs in linear time and works well because node IDs are already within the range [0, n-1]. Conceptually, the structure behaves like an adjacency accumulation in a graph.
Approach 3: Using a Hash Map for Flexibility (O(n) time, O(n) space)
If node identifiers were sparse or outside a predictable range, a hash map would be more flexible than a fixed array. Iterate through edges and update a map where the key is the destination node and the value stores the running score. Each iteration performs a constant-time hash lookup and addition. After building the map, iterate through the keys to determine the node with the maximum score. This approach uses the same accumulation idea as the array method but relies on a hash table for dynamic storage. It’s slightly heavier in memory and constant factors but easier to adapt when node IDs are not bounded.
Recommended for interviews: The array accumulation approach is the expected solution. It demonstrates that you recognize the fixed node range and can compute scores in one linear pass. Mentioning the brute force method first shows understanding of the scoring definition, while transitioning to the O(n) array solution shows optimization skills. The hash map version is useful to discuss when the problem generalizes beyond a fixed-size node set.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Score Calculation | O(n²) | O(1) | Useful for understanding the definition of edge score or when constraints are extremely small |
| Array Score Accumulation | O(n) | O(n) | Best choice when node IDs are within 0..n-1 and you want the fastest solution |
| Hash Map Accumulation | O(n) | O(n) | Preferred when node identifiers are sparse or not limited to a fixed range |