Node With Highest Edge Score

Medium

Description

Given a directed graph where each node has exactly one outgoing edge, return the node with the highest edge score (sum of nodes pointing to it).

Examples

Input:edges = [1,0,0,0,0,7,7,5]
Output:7
Explanation:

Node 7 has highest score.

Input:edges = [1]
Output:1
Explanation:

Edge case with a single-element array.

Input:edges = [2,2,3,4,2,1]
Output:2
Explanation:

Node 0 points to node 2 (score +0), node 1 points to node 2 (score +1), node 2 points to node 3 (score +2), node 3 points to node 4 (score +3), node 4 points to node 2 (score +4), node 5 points to node 1 (score +5). Node scores: node 1 gets 5, node 2 gets 0+1+4=5, node 3 gets 2, node 4 gets 3. Nodes 1 and 2 tie with score 5, but node 2 has the smaller index so it's returned.

Constraints

  • 2 ≤ n ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!