Node With Highest Edge Score - Counting [JS]

Description 

Solution: Counting

Populate score[i], where i is the sum of node values that have an edge to node i.
Get the node with the highest score.

Time Complexity: O(n) 135ms
Space Complexity: O(n) 56.4MB

var edgeScore = function(edges) {
  let n = edges.length, score = Array(n).fill(0);
  for (let i = 0; i < n; i++) {
    score[edges[i]] += i;
  }
  let ans = 0;
  for (let i = 1; i < n; i++) {
    if (score[i] > score[ans]) {
      ans = i;
    }
  }
  return ans;
};

javascript

Comments

Popular posts from this blog

Minimum Number of Operations to Sort a Binary Tree by Level - BFS & Cycle Counting Explained [JS]

Beautiful Towers II - Monotonic Increasing Stack [JS]

Reschedule Meetings for Maximum Free Time I - Sliding Window - Constant Space [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]