{"id":9032,"date":"2021-12-05T09:36:35","date_gmt":"2021-12-05T17:36:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9032"},"modified":"2021-12-05T18:17:27","modified_gmt":"2021-12-06T02:17:27","slug":"leetcode-2097-valid-arrangement-of-pairs","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2097-valid-arrangement-of-pairs\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2097. Valid Arrangement of Pairs"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>pairs<\/code>&nbsp;where&nbsp;<code>pairs[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>. An arrangement of&nbsp;<code>pairs<\/code>&nbsp;is&nbsp;<strong>valid<\/strong>&nbsp;if for every index&nbsp;<code>i<\/code>&nbsp;where&nbsp;<code>1 &lt;= i &lt; pairs.length<\/code>, we have&nbsp;<code>end<sub>i-1<\/sub>&nbsp;== start<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em><strong>any<\/strong>&nbsp;valid arrangement of&nbsp;<\/em><code>pairs<\/code>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The inputs will be generated such that there exists a valid arrangement of&nbsp;<code>pairs<\/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> pairs = [[5,1],[4,5],[11,9],[9,4]]\n<strong>Output:<\/strong> [[11,9],[9,4],[4,5],[5,1]]\n<strong>Explanation:\n<\/strong>This is a valid arrangement since end<sub>i-1<\/sub> always equals start<sub>i<\/sub>.\nend<sub>0<\/sub> = 9 == 9 = start<sub>1<\/sub> \nend<sub>1<\/sub> = 4 == 4 = start<sub>2<\/sub>\nend<sub>2<\/sub> = 5 == 5 = start<sub>3<\/sub>\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> pairs = [[1,3],[3,2],[2,1]]\n<strong>Output:<\/strong> [[1,3],[3,2],[2,1]]\n<strong>Explanation:<\/strong>\nThis is a valid arrangement since end<sub>i-1<\/sub> always equals start<sub>i<\/sub>.\nend<sub>0<\/sub> = 3 == 3 = start<sub>1<\/sub>\nend<sub>1<\/sub> = 2 == 2 = start<sub>2<\/sub>\nThe arrangements [[2,1],[1,3],[3,2]] and [[3,2],[2,1],[1,3]] are also valid.\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> pairs = [[1,2],[1,3],[2,1]]\n<strong>Output:<\/strong> [[1,2],[2,1],[1,3]]\n<strong>Explanation:<\/strong>\nThis is a valid arrangement since end<sub>i-1<\/sub> always equals start<sub>i<\/sub>.\nend<sub>0<\/sub> = 2 == 2 = start<sub>1<\/sub>\nend<sub>1<\/sub> = 1 == 1 = start<sub>2<\/sub>\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= pairs.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>pairs[i].length == 2<\/code><\/li><li><code>0 &lt;= start<sub>i<\/sub>, end<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>start<sub>i<\/sub>&nbsp;!= end<sub>i<\/sub><\/code><\/li><li>No two pairs are exactly the same.<\/li><li>There&nbsp;<strong>exists<\/strong>&nbsp;a valid arrangement of&nbsp;<code>pairs<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Eulerian trail<\/strong><\/h2>\n\n\n\n<p>The goal of the problem is to find a Eulerian trail in the graph.<\/p>\n\n\n\n<p>If there is a vertex whose out degree &#8211; in degree == 1 which means it&#8217;s the starting vertex. Otherwise wise, the graph must have a Eulerian circuit thus we can start from any vertex.<\/p>\n\n\n\n<p>We can use Hierholzer&#8217;s algorithm to find it.<\/p>\n\n\n\n<p>Time complexity: O(|V| + |E|)<br>Space complexity: O(|V| + |E|)<\/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  vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {    \n    unordered_map<int, queue<int>> g;\n    unordered_map<int, int> degree; \/\/ out - in  \n    for (const auto& p : pairs) {\n      g[p[0]].push(p[1]);\n      ++degree[p[0]];\n      --degree[p[1]];\n    }\n    \n    int s = pairs[0][0];\n    for (const auto& [u, d] : degree)\n      if (d == 1) s = u;\n    \n    vector<vector<int>> ans;\n    function<void(int)> dfs = [&](int u) {\n      while (!g[u].empty()) {\n        int v = g[u].front(); g[u].pop();\n        dfs(v);\n        ans.push_back({u, v});\n      }\n    };\n    dfs(s);\n    return {rbegin(ans), rend(ans)};\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def validArrangement(self, pairs: List[List[int]]) -> List[List[int]]:\n    g = defaultdict(list)\n    d = defaultdict(int)\n    for u, v in pairs:\n      g[u].append(v)\n      d[u] += 1\n      d[v] -= 1\n    \n    s = pairs[0][0]\n    for u in d:\n      if d[u] == 1: s = u\n    \n    ans = []\n    def dfs(u: int) -> None:\n      while g[u]:\n        v = g[u].pop()\n        dfs(v)\n        ans.append([u, v])\n\n    dfs(s)\n    return ans[::-1]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;2D integer array&nbsp;pairs&nbsp;where&nbsp;pairs[i] = [starti, endi]. An arrangement of&nbsp;pairs&nbsp;is&nbsp;valid&nbsp;if for every index&nbsp;i&nbsp;where&nbsp;1 &lt;= i &lt; pairs.length, we have&nbsp;endi-1&nbsp;== starti. Return&nbsp;any&nbsp;valid arrangement of&nbsp;pairs.&#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":[741,77],"class_list":["post-9032","post","type-post","status-publish","format-standard","hentry","category-graph","tag-eulerian","tag-graph","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9032","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=9032"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9032\/revisions"}],"predecessor-version":[{"id":9037,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9032\/revisions\/9037"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}