<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eulerian Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/eulerian/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/eulerian/</link>
	<description></description>
	<lastBuildDate>Mon, 06 Dec 2021 02:17:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>eulerian Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/eulerian/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2097. Valid Arrangement of Pairs</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-2097-valid-arrangement-of-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-2097-valid-arrangement-of-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Dec 2021 17:36:35 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[eulerian]]></category>
		<category><![CDATA[graph]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9032</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;2D integer array&#160;pairs&#160;where&#160;pairs[i] = [starti, endi]. An arrangement of&#160;pairs&#160;is&#160;valid&#160;if for every index&#160;i&#160;where&#160;1 &#60;= i &#60; pairs.length, we have&#160;endi-1&#160;== starti. Return&#160;any&#160;valid arrangement of&#160;pairs.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-2097-valid-arrangement-of-pairs/">花花酱 LeetCode 2097. Valid Arrangement of Pairs</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p>Return&nbsp;<em><strong>any</strong>&nbsp;valid arrangement of&nbsp;</em><code>pairs</code>.</p>



<p><strong>Note:</strong>&nbsp;The inputs will be generated such that there exists a valid arrangement of&nbsp;<code>pairs</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pairs = [[5,1],[4,5],[11,9],[9,4]]
<strong>Output:</strong> [[11,9],[9,4],[4,5],[5,1]]
<strong>Explanation:
</strong>This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>.
end<sub>0</sub> = 9 == 9 = start<sub>1</sub> 
end<sub>1</sub> = 4 == 4 = start<sub>2</sub>
end<sub>2</sub> = 5 == 5 = start<sub>3</sub>
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pairs = [[1,3],[3,2],[2,1]]
<strong>Output:</strong> [[1,3],[3,2],[2,1]]
<strong>Explanation:</strong>
This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>.
end<sub>0</sub> = 3 == 3 = start<sub>1</sub>
end<sub>1</sub> = 2 == 2 = start<sub>2</sub>
The arrangements [[2,1],[1,3],[3,2]] and [[3,2],[2,1],[1,3]] are also valid.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> pairs = [[1,2],[1,3],[2,1]]
<strong>Output:</strong> [[1,2],[2,1],[1,3]]
<strong>Explanation:</strong>
This is a valid arrangement since end<sub>i-1</sub> always equals start<sub>i</sub>.
end<sub>0</sub> = 2 == 2 = start<sub>1</sub>
end<sub>1</sub> = 1 == 1 = start<sub>2</sub>
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Eulerian trail</strong></h2>



<p>The goal of the problem is to find a Eulerian trail in the graph.</p>



<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>



<p>We can use Hierholzer&#8217;s algorithm to find it.</p>



<p>Time complexity: O(|V| + |E|)<br>Space complexity: O(|V| + |E|)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; validArrangement(vector&lt;vector&lt;int&gt;&gt;&amp; pairs) {    
    unordered_map&lt;int, queue&lt;int&gt;&gt; g;
    unordered_map&lt;int, int&gt; degree; // out - in  
    for (const auto&amp; p : pairs) {
      g[p[0]].push(p[1]);
      ++degree[p[0]];
      --degree[p[1]];
    }
    
    int s = pairs[0][0];
    for (const auto&amp; [u, d] : degree)
      if (d == 1) s = u;
    
    vector&lt;vector&lt;int&gt;&gt; ans;
    function&lt;void(int)&gt; dfs = [&amp;](int u) {
      while (!g[u].empty()) {
        int v = g[u].front(); g[u].pop();
        dfs(v);
        ans.push_back({u, v});
      }
    };
    dfs(s);
    return {rbegin(ans), rend(ans)};
  }
};</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def validArrangement(self, pairs: List[List[int]]) -&gt; List[List[int]]:
    g = defaultdict(list)
    d = defaultdict(int)
    for u, v in pairs:
      g[u].append(v)
      d[u] += 1
      d[v] -= 1
    
    s = pairs[0][0]
    for u in d:
      if d[u] == 1: s = u
    
    ans = []
    def dfs(u: int) -&gt; None:
      while g[u]:
        v = g[u].pop()
        dfs(v)
        ans.append([u, v])

    dfs(s)
    return ans[::-1]</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-2097-valid-arrangement-of-pairs/">花花酱 LeetCode 2097. Valid Arrangement of Pairs</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-2097-valid-arrangement-of-pairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
