Node With Highest Edge Score

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
Inputedges = [1,0,0,0,0,7,7,5]
Output7
Node 7 has the highest edge score, since nodes 5 and 6 point to it for a score of 11.
Example 2
Inputedges = [2,0,0,2]
Output0
Nodes 0 and 2 both have an edge score of 3, so node 0 is returned because it has the smaller index.

Constraints

  • n == edges.length
  • 2 <= n <= 10^5
  • 0 <= edges[i] < n
  • edges[i] != i

Asked at 1 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate