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] != iWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are tracking a game where players pass a ball to each other and you need to figure out who received the most total points from incoming passes. The brute force approach involves looking at every single person one by one, gathering all the points they received, and comparing everyone's totals.
Here's how the algorithm would work step-by-step:
def highest_edge_score(outgoing_edges):
total_nodes_count = len(outgoing_edges)
highest_score_found = -1
best_node_identifier = -1
# Evaluate each node individually to calculate its total incoming score from scratch
for current_target_node in range(total_nodes_count):
accumulated_edge_score = 0
# Scan the entire edge list to find every incoming pass directed to this node
for source_node_index in range(total_nodes_count):
if outgoing_edges[source_node_index] == current_target_node:
accumulated_edge_score += source_node_index
# Update our best candidate if this node strictly beats the previous maximum score
if accumulated_edge_score > highest_score_found:
highest_score_found = accumulated_edge_score
best_node_identifier = current_target_node
return best_node_identifierTo find the node with the highest edge score efficiently, we can calculate and track the total incoming points for every single node in a single pass. By keeping a running total of the points each node receives from others, we can easily identify the winner without having to constantly recheck the connections.
Here's how the algorithm would work step-by-step:
def edge_score(edges):
total_nodes = len(edges)
score_board = [0] * total_nodes
# Accumulate incoming edge scores for each node in a single pass
for source_node, target_node in enumerate(edges):
score_board[target_node] += source_node
highest_score = -1
best_node_identifier = -1
# Track the maximum score while favoring the smaller node index on ties
for node_identifier, current_score in enumerate(score_board):
if current_score > highest_score:
highest_score = current_score
best_node_identifier = node_identifier
# Return the node with the highest accumulated score
return best_node_identifier| Case | How to Handle |
|---|---|
| Minimum valid input size where the edges array has only one element | The loop processes the single edge, adding the source node to the target node's score and immediately returning that target node. |
| Multiple nodes achieving the exact same maximum edge score | We iterate through the accumulated scores from smallest node identifier to largest, ensuring we select the smaller identifier upon a tie. |
| A single node receives all incoming edges from every other node | The score accumulation array correctly sums all incoming node labels for that single target without index out of bounds issues. |
| Graph contains self-loops where a node points to itself | The algorithm adds the node's own label to its edge score just like any other incoming edge. |
| Maximum possible node identifiers and edge counts up to the problem constraints | We use a 64-bit integer array or hash map for score storage to prevent potential integer overflow from large summations. |
| Nodes with zero incoming edges receiving no score updates | These nodes remain at a score of zero and naturally lose out during the maximum score search unless all scores are zero. |
| Disconnected or isolated nodes that never appear as edge targets | Since scores default to zero, these nodes are simply ignored when finding the maximum edge score. |
| Graph contains cycles among multiple nodes | Since we only sum incoming edges and do not traverse paths recursively, cycles do not cause infinite loops or stack overflows. |