Skip to content

2374. Node With Highest Edge Score 👍

  • Time: $O(n)$
  • Space: $O(n)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  int edgeScore(vector<int>& edges) {
    vector<long> scores(edges.size());
    for (int i = 0; i < edges.size(); ++i)
      scores[edges[i]] += i;
    return ranges::max_element(scores) - scores.begin();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  public int edgeScore(int[] edges) {
    int ans = 0;
    long[] scores = new long[edges.length];

    for (int i = 0; i < edges.length; ++i)
      scores[edges[i]] += i;

    for (int i = 1; i < scores.length; ++i)
      if (scores[i] > scores[ans])
        ans = i;

    return ans;
  }
}
1
2
3
4
5
6
class Solution:
  def edgeScore(self, edges: list[int]) -> int:
    scores = [0] * len(edges)
    for i, edge in enumerate(edges):
      scores[edge] += i
    return scores.index(max(scores))