2374. Node With Highest Edge Score Medium

@problem@discussion
#Hash Table#Graph



1/**
2 * [2374] Node With Highest Edge Score
3 *
4 * You are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge.
5 * 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].
6 * 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.
7 * Return the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index.
8 *  
9 * Example 1:
10 * <img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png" style="width: 450px; height: 260px;" />
11 * Input: edges = [1,0,0,0,0,7,7,5]
12 * Output: 7
13 * Explanation:
14 * - 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.
15 * - The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.
16 * - The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.
17 * - The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.
18 * Node 7 has the highest edge score so return 7.
19 * 
20 * Example 2:
21 * <img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png" style="width: 150px; height: 155px;" />
22 * Input: edges = [2,0,0,2]
23 * Output: 0
24 * Explanation:
25 * - The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.
26 * - The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.
27 * Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	n == edges.length
33 * 	2 <= n <= 10^5
34 * 	0 <= edges[i] < n
35 * 	edges[i] != i
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/node-with-highest-edge-score/
41// discuss: https://leetcode.com/problems/node-with-highest-edge-score/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn edge_score(edges: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2374() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.