<?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>gray code Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/gray-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/gray-code/</link>
	<description></description>
	<lastBuildDate>Mon, 28 Oct 2019 03:25:15 +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>gray code Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/gray-code/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1238. Circular Permutation in Binary Representation</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 28 Oct 2019 03:22:14 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[gray code]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[rotation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5788</guid>

					<description><![CDATA[<p>Given 2 integers&#160;n&#160;and&#160;start. Your task is return&#160;any&#160;permutation&#160;p&#160;of&#160;(0,1,2.....,2^n -1)&#160;such that : p[0] = start p[i]&#160;and&#160;p[i+1]&#160;differ by only one bit in their binary representation. p[0]&#160;and&#160;p[2^n -1]&#160;must also&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/">花花酱 LeetCode 1238. Circular Permutation in Binary Representation</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>Given 2 integers&nbsp;<code>n</code>&nbsp;and&nbsp;<code>start</code>. Your task is return&nbsp;<strong>any</strong>&nbsp;permutation&nbsp;<code>p</code>&nbsp;of&nbsp;<code>(0,1,2.....,2^n -1)&nbsp;</code>such that :</p>



<ul><li><code>p[0] = start</code></li><li><code>p[i]</code>&nbsp;and&nbsp;<code>p[i+1]</code>&nbsp;differ by only one bit in their binary representation.</li><li><code>p[0]</code>&nbsp;and&nbsp;<code>p[2^n -1]</code>&nbsp;must also differ by only one bit in their binary representation.</li></ul>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 2, start = 3
<strong>Output:</strong> [3,2,0,1]
<strong>Explanation:</strong> The binary representation of the permutation is (11,10,00,01). 
All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, start = 2
<strong>Output:</strong> [2,6,7,5,4,0,1,3]
<strong>Explanation:</strong> The binary representation of the permutation is (010,110,111,101,100,000,001,011).
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 16</code></li><li><code>0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n</code></li></ul>



<p>Solution 1: Gray Code (DP) + Rotation</p>



<p>Gray code starts with 0, need to rotate after generating the list.<br><br>Time complexity: O(2^n)<br>Space complexity: O(2^n)</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;int&gt; circularPermutation(int n, int start) {
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1);
    dp[0] = {0};    
    for (int i = 1; i &lt;= n; ++i) {
      dp[i] = dp[i - 1];
      for (int j = dp[i - 1].size() - 1; j &gt;= 0; --j)
        dp[i].push_back(dp[i - 1][j] | (1 &lt;&lt; (i - 1)));
    }
    for (auto it = begin(dp[n]); it != end(dp[n]); ++it)
      if (*it == start) {
        rotate(begin(dp[n]), it, end(dp[n]));
        break;
      }
    return dp[n];
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Gray code with a start</strong></h2>



<p>Time complexity: O(2^n)<br>Space complexity: O(1)</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;int&gt; circularPermutation(int n, int start) {
    vector&lt;int&gt; ans(1 &lt;&lt; n);
    for (int i = 0; i &lt; 1 &lt;&lt; n; ++i)
      ans[i] = start ^ i ^ i &gt;&gt; 1;
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/">花花酱 LeetCode 1238. Circular Permutation in Binary Representation</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/dynamic-programming/leetcode-1238-circular-permutation-in-binary-representation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
