{"id":4885,"date":"2019-02-23T23:05:15","date_gmt":"2019-02-24T07:05:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4885"},"modified":"2019-02-23T23:05:48","modified_gmt":"2019-02-24T07:05:48","slug":"leetcode-997-find-the-town-judge","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-997-find-the-town-judge\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 997. Find the Town Judge"},"content":{"rendered":"\n<p>In a town, there are&nbsp;<code>N<\/code>&nbsp;people labelled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>N<\/code>.&nbsp; There is a rumor that one of these people is secretly the town judge.<\/p>\n\n\n\n<p>If the&nbsp;town judge exists, then:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The town judge trusts nobody.<\/li><li>Everybody (except for the town judge) trusts the town judge.<\/li><li>There is exactly one person that satisfies properties 1 and 2.<\/li><\/ol>\n\n\n\n<p>You are given&nbsp;<code>trust<\/code>, an array of pairs&nbsp;<code>trust[i] = [a, b]<\/code>&nbsp;representing that the person labelled&nbsp;<code>a<\/code>&nbsp;trusts the person labelled&nbsp;<code>b<\/code>.<\/p>\n\n\n\n<p>If the town judge exists and can be identified, return the label of the town judge.&nbsp; Otherwise, return&nbsp;<code>-1<\/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>N = 2, trust = [[1,2]]\n<strong>Output: <\/strong>2\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>N = 3, trust = [[1,3],[2,3]]\n<strong>Output: <\/strong>3\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>N = 3, trust = [[1,3],[2,3],[3,1]]\n<strong>Output: <\/strong>-1\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>N = 3, trust = [[1,2],[2,3]]\n<strong>Output: <\/strong>-1\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]\n<strong>Output: <\/strong>3<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= N &lt;= 1000<\/code><\/li><li><code>trust.length &lt;= 10000<\/code><\/li><li><code>trust[i]<\/code>&nbsp;are all different<\/li><li><code>trust[i][0] != trust[i][1]<\/code><\/li><li><code>1 &lt;= trust[i][0], trust[i][1] &lt;= N<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Degree<\/strong><\/h2>\n\n\n\n<p>node with degree (in_degree &#8211; out_degree) N &#8211; 1 is the judge.<\/p>\n\n\n\n<p>Time complexity: O(N+T)<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++\">\nclass Solution {\npublic:\n  int findJudge(int N, vector<vector<int>>& trusts) {    \n    vector<int> degrees(N + 1);    \n    for (const auto& trust : trusts) {\n      --degrees[trust[0]];\n      ++degrees[trust[1]];\n    }\n    for (int i = 1; i <= N; ++i)\n      if (degrees[i] == N - 1) return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a town, there are&nbsp;N&nbsp;people labelled from&nbsp;1&nbsp;to&nbsp;N.&nbsp; There is a rumor that one of these people is secretly the town judge. If the&nbsp;town judge exists,&#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":[244,222,77],"class_list":["post-4885","post","type-post","status-publish","format-standard","hentry","category-graph","tag-degree","tag-easy","tag-graph","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4885","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=4885"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4885\/revisions"}],"predecessor-version":[{"id":4887,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4885\/revisions\/4887"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}