{"id":5788,"date":"2019-10-27T20:22:14","date_gmt":"2019-10-28T03:22:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5788"},"modified":"2019-10-27T20:25:15","modified_gmt":"2019-10-28T03:25:15","slug":"leetcode-1238-circular-permutation-in-binary-representation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1238-circular-permutation-in-binary-representation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1238. Circular Permutation in Binary Representation"},"content":{"rendered":"\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\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, start = 3\n<strong>Output:<\/strong> [3,2,0,1]\n<strong>Explanation:<\/strong> The binary representation of the permutation is (11,10,00,01). \nAll the adjacent element differ by one bit. Another valid permutation is [3,1,0,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, start = 2\n<strong>Output:<\/strong> [2,6,7,5,4,0,1,3]\n<strong>Explanation:<\/strong> The binary representation of the permutation is (010,110,111,101,100,000,001,011).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 16<\/code><\/li><li><code>0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n<\/code><\/li><\/ul>\n\n\n\n<p>Solution 1: Gray Code (DP) + Rotation<\/p>\n\n\n\n<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>\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<int> circularPermutation(int n, int start) {\n    vector<vector<int>> dp(n + 1);\n    dp[0] = {0};    \n    for (int i = 1; i <= n; ++i) {\n      dp[i] = dp[i - 1];\n      for (int j = dp[i - 1].size() - 1; j >= 0; --j)\n        dp[i].push_back(dp[i - 1][j] | (1 << (i - 1)));\n    }\n    for (auto it = begin(dp[n]); it != end(dp[n]); ++it)\n      if (*it == start) {\n        rotate(begin(dp[n]), it, end(dp[n]));\n        break;\n      }\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Gray code with a start<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(2^n)<br>Space complexity: O(1)<\/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<int> circularPermutation(int n, int start) {\n    vector<int> ans(1 << n);\n    for (int i = 0; i < 1 << n; ++i)\n      ans[i] = start ^ i ^ i >> 1;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given 2 integers&nbsp;n&nbsp;and&nbsp;start. Your task is return&nbsp;any&nbsp;permutation&nbsp;p&nbsp;of&nbsp;(0,1,2&#8230;..,2^n -1)&nbsp;such that : p[0] = start p[i]&nbsp;and&nbsp;p[i+1]&nbsp;differ by only one bit in their binary representation. p[0]&nbsp;and&nbsp;p[2^n -1]&nbsp;must also&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[16,514,177,367],"class_list":["post-5788","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-bit","tag-gray-code","tag-medium","tag-rotation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5788","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=5788"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5788\/revisions"}],"predecessor-version":[{"id":5791,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5788\/revisions\/5791"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}