Node With Highest Edge Score LeetCode Solution

Problem – Node With Highest Edge Score LeetCode Solution

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:

Input: edges = [1,0,0,0,0,7,7,5]
Output: 7
Explanation:
- 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.
- The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.
- The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.
- The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.
Node 7 has the highest edge score so return 7.

Example 2:

Input: edges = [2,0,0,2]
Output: 0
Explanation:
- The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.
- The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.
Nodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.

Constraints:

  • n == edges.length
  • 2 <= n <= 105
  • 0 <= edges[i] < n
  • edges[i] != i

Node With Highest Edge Score LeetCode Solution in C++

int edgeScore(vector<int>& edges) {
    vector<long long> score(edges.size());
    for (int i = 0; i < edges.size(); ++i)
        score[edges[i]] += i;
    return max_element(begin(score), end(score)) - begin(score);
}

Node With Highest Edge Score LeetCode Solution in Java

class Solution {
    public int edgeScore(int[] edges) {
        int n_length=edges.length;
		//to avoid overflow we will take long size score array 
        long score_of_node[]=new long[n_length];
        for(int i=0; i<n_length; i++){
            score_of_node[edges[i]]+=i; //score of the node will be update to (prev score + incomming node) here every i denotes income incomming vertex
        }
        int max_ans_index=0;
        
        for(int i=0; i<n_length; i++){
            if(score_of_node[i]>score_of_node[max_ans_index]){
                max_ans_index=i; // simply just find the node with maximum score and
            }
        }
        return max_ans_index; // return the node
    }
}

Node With Highest Edge Score LeetCode Solution in Python

class Solution:
    def edgeScore(self, edges: List[int]) -> int:

        n = len(edges)
        cnt = defaultdict(int)
        ans = 0
        
        for i in range(n):
            cnt[edges[i]] += i

        m = max(cnt.values())

        for i in range(n):
            if cnt[i] == m:
                ans = i
                break
        
        return ans
Node With Highest Edge Score LeetCode Solution Review:

In our experience, we suggest you solve this Node With Highest Edge Score LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Node With Highest Edge Score LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Node With Highest Edge Score LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.