#2374
Medium Algorithms Node with highest edge score
Hash Table Graph Theory
49.2% acceptance
Feb 25, 2026
485
43
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn edge_score(edges: Vec<i32>) -> i32 {
let n = edges.len();
let mut score = vec![0i64; n];
for (i, &e) in edges.iter().enumerate() {
score[e as usize] += i as i64;
}
let mut best = 0usize;
for i in 1..n {
if score[i] > score[best] { best = i; }
}
best as i32
}
}