Node With Highest Edge Score - Counting [JS]
- Get link
- X
- Other Apps
By
Anna An
on
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
- Get link
- X
- Other Apps
Comments
Post a Comment