{"id":9662,"date":"2022-04-16T23:16:40","date_gmt":"2022-04-17T06:16:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9662"},"modified":"2022-04-17T14:19:36","modified_gmt":"2022-04-17T21:19:36","slug":"leetcode-2242-maximum-score-of-a-node-sequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2242-maximum-score-of-a-node-sequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2242. Maximum Score of a Node Sequence"},"content":{"rendered":"\n<p>There is an&nbsp;<strong>undirected<\/strong>&nbsp;graph with&nbsp;<code>n<\/code>&nbsp;nodes, numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>.<\/p>\n\n\n\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>scores<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>scores[i]<\/code>&nbsp;denotes the score of node&nbsp;<code>i<\/code>. You are also given a 2D integer array&nbsp;<code>edges<\/code>&nbsp;where&nbsp;<code>edges[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;denotes that there exists an&nbsp;<strong>undirected<\/strong>&nbsp;edge connecting nodes&nbsp;<code>a<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>b<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>A node sequence is&nbsp;<strong>valid<\/strong>&nbsp;if it meets the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There is an edge connecting every pair of&nbsp;<strong>adjacent<\/strong>&nbsp;nodes in the sequence.<\/li><li>No node appears more than once in the sequence.<\/li><\/ul>\n\n\n\n<p>The score of a node sequence is defined as the&nbsp;<strong>sum<\/strong>&nbsp;of the scores of the nodes in the sequence.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum score<\/strong>&nbsp;of a valid node sequence with a length of&nbsp;<\/em><code>4<\/code><em>.&nbsp;<\/em>If no such sequence exists, return<em>&nbsp;<\/em><code>-1<\/code>.<\/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\/04\/15\/ex1new3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]\n<strong>Output:<\/strong> 24\n<strong>Explanation:<\/strong> The figure above shows the graph and the chosen node sequence [0,1,2,3].\nThe score of the node sequence is 5 + 2 + 9 + 8 = 24.\nIt can be shown that no other node sequence has a score of more than 24.\nNote that the sequences [3,1,2,0] and [1,0,2,3] are also valid and have a score of 24.\nThe sequence [0,3,2,4] is not valid since no edge connects nodes 0 and 3.\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\/03\/17\/ex2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> The figure above shows the graph.\nThere are no valid node sequences of length 4, so we return -1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == scores.length<\/code><\/li><li><code>4 &lt;= n &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= scores[i] &lt;= 10<sup>8<\/sup><\/code><\/li><li><code>0 &lt;= edges.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>0 &lt;= a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt;= n - 1<\/code><\/li><li><code>a<sub>i<\/sub>&nbsp;!= b<sub>i<\/sub><\/code><\/li><li>There are no duplicate edges.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy \/ Top3 neighbors<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-1.png\" alt=\"\" class=\"wp-image-9667\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-2.png\" alt=\"\" class=\"wp-image-9668\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-3.png\" alt=\"\" class=\"wp-image-9669\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-4.png\" alt=\"\" class=\"wp-image-9671\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-4.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-4-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-4-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-5.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-5.png\" alt=\"\" class=\"wp-image-9673\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-5.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-5-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2022\/04\/lc2242-ep396-5-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Since |E| is already 5*10<sup>4<\/sup>, we can&#8217;t enumerate all possible sequences. We must do in O(|E|) or O(|E|log|E|).<\/p>\n\n\n\n<p>Enumerate all the edges, we have a pair of node a, b. To get the optimal answer, we just need to find the largest neighbor of a and b, which we call c, d respectively. Just need to make sure a, b, c, d are unique. i.e. c != d, c != b and d != a. Since the a&#8217;s largest neighbor can be either b or d. We can&#8217;t just store the largest neighbor, but top 3 instead for each node to avoid duplications.<\/p>\n\n\n\n<p>Time complexity: O(|E|*9)<br>Space complexity: O(|V|*3)<\/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 maximumScore(vector<int>& scores, vector<vector<int>>& edges) {\n    const int n = scores.size();\n    vector<set<pair<int, int>>> top3(n);\n    for (const auto& e : edges) {      \n      top3[e[0]].emplace(scores[e[1]], e[1]);\n      top3[e[1]].emplace(scores[e[0]], e[0]);\n      if (top3[e[0]].size() > 3) top3[e[0]].erase(begin(top3[e[0]]));\n      if (top3[e[1]].size() > 3) top3[e[1]].erase(begin(top3[e[1]]));\n    }\n    int ans = -1;\n    for (const auto& e : edges) {\n      const int a = e[0], b = e[1], sa = scores[a], sb = scores[b];\n      for (const auto& [sc, c] : top3[a])\n        for (const auto& [sd, d] : top3[b])          \n          if (sa + sb + sc + sd > ans && c != b && c != d && d != a)\n            ans = sa + sb + sc + sd;\n    }\n    return ans;\n  }\n};\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an&nbsp;undirected&nbsp;graph with&nbsp;n&nbsp;nodes, numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;scores&nbsp;of length&nbsp;n&nbsp;where&nbsp;scores[i]&nbsp;denotes the score of node&nbsp;i. You are also given a 2D integer&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[77,88,217,73,243],"class_list":["post-9662","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-graph","tag-greedy","tag-hard","tag-heap","tag-set","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9662","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=9662"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9662\/revisions"}],"predecessor-version":[{"id":9674,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9662\/revisions\/9674"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}