java.lang.Object
g2301_2400.s2374_node_with_highest_edge_score.Solution

public class Solution extends Object
2374 - Node With Highest Edge Score.<p>Medium</p> <p>You are given a directed graph with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, where each node has <strong>exactly one</strong> outgoing edge.</p> <p>The graph is represented by a given <strong>0-indexed</strong> integer array <code>edges</code> of length <code>n</code>, where <code>edges[i]</code> indicates that there is a <strong>directed</strong> edge from node <code>i</code> to node <code>edges[i]</code>.</p> <p>The <strong>edge score</strong> of a node <code>i</code> is defined as the sum of the <strong>labels</strong> of all the nodes that have an edge pointing to <code>i</code>.</p> <p>Return <em>the node with the highest <strong>edge score</strong></em>. If multiple nodes have the same <strong>edge score</strong> , return the node with the <strong>smallest</strong> index.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620195403-1.png" alt="" /></p> <p><strong>Input:</strong> edges = [1,0,0,0,0,7,7,5]</p> <p><strong>Output:</strong> 7</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>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.</p> </li> <li> <p>The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.</p> </li> <li> <p>The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.</p> </li> <li> <p>The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.</p> </li> </ul> <p>Node 7 has the highest edge score so return 7.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2022/06/20/image-20220620200212-3.png" alt="" /></p> <p><strong>Input:</strong> edges = [2,0,0,2]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.</p> </li> <li> <p>The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.</p> </li> </ul> <p>Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == edges.length</code></li> <li><code>2 <= n <= 10<sup>5</sup></code></li> <li><code>0 <= edges[i] < n</code></li> <li><code>edges[i] != i</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • edgeScore

      public int edgeScore(int[] edges)