Node With Highest Edge Score - Problem
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.
Input & Output
Example 1 — Basic Graph
$
Input:
edges = [1,0,0,2]
›
Output:
0
💡 Note:
Node 0 has incoming edges from nodes 1 and 2, so score = 1 + 2 = 3. Node 1 has incoming edge from node 0, score = 0. Node 2 has incoming edge from node 3, score = 3. Node 0 has the highest score.
Example 2 — Tie Breaker
$
Input:
edges = [1,2,3,0]
›
Output:
0
💡 Note:
Each node has exactly one incoming edge: scores are [3,0,1,2]. Node 0 has the highest score of 3.
Example 3 — Self Loop
$
Input:
edges = [0,1,1]
›
Output:
1
💡 Note:
Node 0 points to itself (score = 0), nodes 1 and 2 point to node 1 (score = 1 + 2 = 3). Node 1 has the highest score.
Constraints
- 2 ≤ edges.length ≤ 105
- 0 ≤ edges[i] < edges.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code