{"id":9787,"date":"2022-08-14T21:36:05","date_gmt":"2022-08-15T04:36:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9787"},"modified":"2022-08-14T21:36:36","modified_gmt":"2022-08-15T04:36:36","slug":"leetcode-2374-node-with-highest-edge-score","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2374-node-with-highest-edge-score\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2374. Node With Highest Edge Score"},"content":{"rendered":"\n<p>You are given a directed graph with&nbsp;<code>n<\/code>&nbsp;nodes labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>, where each node has&nbsp;<strong>exactly one<\/strong>&nbsp;outgoing edge.<\/p>\n\n\n\n<p>The graph is represented by a given&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>edges<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>edges[i]<\/code>&nbsp;indicates that there is a&nbsp;<strong>directed<\/strong>&nbsp;edge from node&nbsp;<code>i<\/code>&nbsp;to node&nbsp;<code>edges[i]<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>edge score<\/strong>&nbsp;of a node&nbsp;<code>i<\/code>&nbsp;is defined as the sum of the&nbsp;<strong>labels<\/strong>&nbsp;of all the nodes that have an edge pointing to&nbsp;<code>i<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the node with the highest&nbsp;<strong>edge score<\/strong><\/em>. If multiple nodes have the same&nbsp;<strong>edge score<\/strong>, return the node with the&nbsp;<strong>smallest<\/strong>&nbsp;index.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/06\/20\/image-20220620195403-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [1,0,0,0,0,7,7,5]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong>\n- 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.\n- The node 0 has an edge pointing to node 1. The edge score of node 1 is 0.\n- The node 7 has an edge pointing to node 5. The edge score of node 5 is 7.\n- The nodes 5 and 6 have an edge pointing to node 7. The edge score of node 7 is 5 + 6 = 11.\nNode 7 has the highest edge score so return 7.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/06\/20\/image-20220620200212-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [2,0,0,2]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong>\n- The nodes 1 and 2 have an edge pointing to node 0. The edge score of node 0 is 1 + 2 = 3.\n- The nodes 0 and 3 have an edge pointing to node 2. The edge score of node 2 is 0 + 3 = 3.\nNodes 0 and 2 both have an edge score of 3. Since node 0 has a smaller index, we return 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == edges.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= edges[i] &lt; n<\/code><\/li><li><code>edges[i] != i<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Use an array to store the score of each node.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>use <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/max_element\">max_element<\/a> to find the largest element.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int edgeScore(vector<int>& edges) {\n    const int n = edges.size();\n    vector<long> s(n);\n    for (int i = 0; i < n; ++i)\n      s[edges[i]] += i;\n    return max_element(begin(s), end(s)) - begin(s);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a directed graph with&nbsp;n&nbsp;nodes labeled from&nbsp;0&nbsp;to&nbsp;n &#8211; 1, where each node has&nbsp;exactly one&nbsp;outgoing edge. The graph is represented by a given&nbsp;0-indexed&nbsp;integer array&nbsp;edges&nbsp;of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[20,77,177],"class_list":["post-9787","post","type-post","status-publish","format-standard","hentry","category-graph","tag-array","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9787"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9787\/revisions"}],"predecessor-version":[{"id":9789,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9787\/revisions\/9789"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}