{"id":5138,"date":"2019-05-04T10:25:32","date_gmt":"2019-05-04T17:25:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5138"},"modified":"2019-05-04T10:37:27","modified_gmt":"2019-05-04T17:37:27","slug":"leetcode-851-loud-and-rich","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-851-loud-and-rich\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 851. Loud and Rich"},"content":{"rendered":"\n<p>In a group of N people (labelled&nbsp;<code>0, 1, 2, ..., N-1<\/code>), each person has different amounts of money, and different levels of quietness.<\/p>\n\n\n\n<p>For convenience, we&#8217;ll call the person with label&nbsp;<code>x<\/code>, simply &#8220;person&nbsp;<code>x<\/code>&#8220;.<\/p>\n\n\n\n<p>We&#8217;ll say that&nbsp;<code>richer[i] = [x, y]<\/code>&nbsp;if person&nbsp;<code>x<\/code>&nbsp;definitely has more money than person&nbsp;<code>y<\/code>.&nbsp; Note that&nbsp;<code>richer<\/code>&nbsp;may only be a subset of valid observations.<\/p>\n\n\n\n<p>Also, we&#8217;ll say&nbsp;<code>quiet[x] = q<\/code>&nbsp;if person&nbsp;x&nbsp;has quietness&nbsp;<code>q<\/code>.<\/p>\n\n\n\n<p>Now, return&nbsp;<code>answer<\/code>, where&nbsp;<code>answer[x] = y<\/code>&nbsp;if&nbsp;<code>y<\/code>&nbsp;is the least quiet person (that is, the person&nbsp;<code>y<\/code>&nbsp;with the smallest value of&nbsp;<code>quiet[y]<\/code>), among all people&nbsp;who definitely have&nbsp;equal to or more money than person&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]\n<strong>Output: <\/strong>[5,5,2,5,4,5,6,7]\n<strong>Explanation: <\/strong>\nanswer[0] = 5.\nPerson 5 has more money than 3, which has more money than 1, which has more money than 0.\nThe only person who is quieter (has lower quiet[x]) is person 7, but\nit isn't clear if they have more money than person 0.\n\nanswer[7] = 7.\nAmong all people that definitely have equal to or more money than person 7\n(which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x])\nis person 7.\n\nThe other answers can be filled out with similar reasoning.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= quiet.length = N &lt;= 500<\/code><\/li><li><code>0 &lt;= quiet[i] &lt; N<\/code>, all&nbsp;<code>quiet[i]<\/code>&nbsp;are different.<\/li><li><code>0 &lt;= richer.length &lt;= N * (N-1) \/ 2<\/code><\/li><li><code>0 &lt;= richer[i][j] &lt; N<\/code><\/li><li><code>richer[i][0] != richer[i][1]<\/code><\/li><li><code>richer[i]<\/code>&#8216;s are all different.<\/li><li>The&nbsp;observations in&nbsp;<code>richer<\/code>&nbsp;are all logically consistent.<\/li><\/ol>\n\n\n\n<p><strong>Solution: DFS + Memoization<\/strong><\/p>\n\n\n\n<p>For person i , remember the quietest person who is richer than person i.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n)<\/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, running time: 108 ms, 31.6 MB\nclass Solution {\npublic:\n  vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {\n    const int n = quiet.size();\n    vector<vector<int>> g(n);\n    for (const auto& r : richer)\n      g[r[1]].push_back(r[0]);\n    vector<int> ans(n, -1);\n    function<void(int)> dfs = [&](int i) {\n      if (ans[i] != -1) return;\n      ans[i] = i;\n      for (int j : g[i]) {\n        dfs(j);\n        if (quiet[ans[j]] < quiet[ans[i]])\n          ans[i] = ans[j];\n      }      \n    };\n    for (int i = 0; i < n; ++i) dfs(i);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a group of N people (labelled&nbsp;0, 1, 2, &#8230;, N-1), each person has different amounts of money, and different levels of quietness. For convenience,&#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":[33,77],"class_list":["post-5138","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5138","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=5138"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5138\/revisions"}],"predecessor-version":[{"id":5141,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5138\/revisions\/5141"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}