{"id":6695,"date":"2020-05-03T20:51:18","date_gmt":"2020-05-04T03:51:18","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6695"},"modified":"2020-05-03T20:51:28","modified_gmt":"2020-05-04T03:51:28","slug":"leetcode-1436-destination-city","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1436-destination-city\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1436. Destination City"},"content":{"rendered":"\n<p>You are given the array&nbsp;<code>paths<\/code>, where&nbsp;<code>paths[i] = [cityA<sub>i<\/sub>, cityB<sub>i<\/sub>]<\/code>&nbsp;means there&nbsp;exists a direct path going from&nbsp;<code>cityA<sub>i<\/sub><\/code>&nbsp;to&nbsp;<code>cityB<sub>i<\/sub><\/code>.&nbsp;<em>Return the destination city, that is, the city without any path outgoing to another city.<\/em><\/p>\n\n\n\n<p>It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.<\/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> paths = [[\"London\",\"New York\"],[\"New York\",\"Lima\"],[\"Lima\",\"Sao Paulo\"]]\n<strong>Output:<\/strong> \"Sao Paulo\" \n<strong>Explanation:<\/strong> Starting at \"London\" city you will reach \"Sao Paulo\" city which is the destination city. Your trip consist of: \"London\" -&gt; \"New York\" -&gt; \"Lima\" -&gt; \"Sao Paulo\".\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> paths = [[\"B\",\"C\"],[\"D\",\"B\"],[\"C\",\"A\"]]\n<strong>Output:<\/strong> \"A\"\n<strong>Explanation:<\/strong> All possible trips are:&nbsp;\n\"D\" -&gt; \"B\" -&gt; \"C\" -&gt; \"A\".&nbsp;\n\"B\" -&gt; \"C\" -&gt; \"A\".&nbsp;\n\"C\" -&gt; \"A\".&nbsp;\n\"A\".&nbsp;\nClearly the destination city is \"A\".\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> paths = [[\"A\",\"Z\"]]\n<strong>Output:<\/strong> \"Z\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= paths.length &lt;= 100<\/code><\/li><li><code>paths[i].length == 2<\/code><\/li><li><code>1 &lt;=&nbsp;cityA<sub>i<\/sub>.length,&nbsp;cityB<sub>i<\/sub>.length &lt;= 10<\/code><\/li><li><code>cityA<sub>i&nbsp;<\/sub>!=&nbsp;cityB<sub>i<\/sub><\/code><\/li><li>All strings&nbsp;consist of lowercase and uppercase English letters and the space character.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Count in \/ out degree for each node<\/strong><\/h2>\n\n\n\n<p>Note: this is a more general solution to this type of problems.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>Destination City: in_degree == 1 and out_degree == 0<\/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  string destCity(vector<vector<string>>& paths) {\n    unordered_map<string, int> in, out;\n    for (const auto& path : paths) {\n      ++out[path[0]];\n      ++in[path[1]];\n    }\n    \n    for (const auto& [city, degree] : in)\n      if (degree == 1 && out[city] == 0) return city;\n    \n    return \"\";\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the array&nbsp;paths, where&nbsp;paths[i] = [cityAi, cityBi]&nbsp;means there&nbsp;exists a direct path going from&nbsp;cityAi&nbsp;to&nbsp;cityBi.&nbsp;Return the destination city, that is, the city without any path&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,77,82],"class_list":["post-6695","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-graph","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6695","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=6695"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6695\/revisions"}],"predecessor-version":[{"id":6697,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6695\/revisions\/6697"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}