{"id":7950,"date":"2021-01-09T20:02:45","date_gmt":"2021-01-10T04:02:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7950"},"modified":"2021-01-09T22:29:50","modified_gmt":"2021-01-10T06:29:50","slug":"leetcode-1718-construct-the-lexicographically-largest-valid-sequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1718-construct-the-lexicographically-largest-valid-sequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1718. Construct the Lexicographically Largest Valid Sequence"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, find a sequence that satisfies all of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The integer&nbsp;<code>1<\/code>&nbsp;occurs once in the sequence.<\/li><li>Each integer between&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>n<\/code>&nbsp;occurs twice in the sequence.<\/li><li>For every integer&nbsp;<code>i<\/code>&nbsp;between&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>n<\/code>, the&nbsp;<strong>distance<\/strong>&nbsp;between the two occurrences of&nbsp;<code>i<\/code>&nbsp;is exactly&nbsp;<code>i<\/code>.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>distance<\/strong>&nbsp;between two numbers on the sequence,&nbsp;<code>a[i]<\/code>&nbsp;and&nbsp;<code>a[j]<\/code>, is the absolute difference of their indices,&nbsp;<code>|j - i|<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>lexicographically largest<\/strong>&nbsp;sequence<\/em><em>. It is guaranteed that under the given constraints, there is always a solution.<\/em><\/p>\n\n\n\n<p>A sequence&nbsp;<code>a<\/code>&nbsp;is lexicographically larger than a sequence&nbsp;<code>b<\/code>&nbsp;(of the same length) if in the first position where&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;differ, sequence&nbsp;<code>a<\/code>&nbsp;has a number greater than the corresponding number in&nbsp;<code>b<\/code>. For example,&nbsp;<code>[0,1,9,0]<\/code>&nbsp;is lexicographically larger than&nbsp;<code>[0,1,5,6]<\/code>&nbsp;because the first position they differ is at the third number, and&nbsp;<code>9<\/code>&nbsp;is greater than&nbsp;<code>5<\/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> n = 3\n<strong>Output:<\/strong> [3,1,2,3,2]\n<strong>Explanation:<\/strong> [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.\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 = 5\n<strong>Output:<\/strong> [5,3,1,4,3,5,2,4,2]\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;= 20<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Search<\/strong><\/h2>\n\n\n\n<p>Search from left to right, largest to smallest.<\/p>\n\n\n\n<p>Time complexity: O(n!)?<br>Space complexity: O(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\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> constructDistancedSequence(int n) {\n    const int len = 2 * n - 1;\n    vector<int> ans(len);    \n    function<bool(int, int)> dfs = [&](int i, int s) {      \n      if (i == len) return true;\n      if (ans[i]) return dfs(i + 1, s);\n      for (int d = n; d > 0; --d) {\n        const int j = i + (d == 1 ? 0 : d);\n        if ((s & (1 << d)) || j >= len || ans[j]) continue;\n        ans[i] = ans[j] = d;\n        if (dfs(i + 1, s | (1 << d))) return true;\n        ans[i] = ans[j] = 0;\n      }\n      return false;\n    };\n    dfs(0, 0);\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  private boolean dfs(int[] ans, int n, int i, int s) {\n    if (i == ans.length) return true;\n    if (ans[i] > 0) return dfs(ans, n, i + 1, s);\n    for (int d = n; d > 0; --d) {\n      int j = i + (d == 1 ? 0 : d);\n      if ((s & (1 << d)) > 0 || j >= ans.length || ans[j] > 0)\n        continue;\n      ans[i] = ans[j] = d;\n      if (dfs(ans, n, i + 1, s | (1 << d))) return true;\n      ans[i] = ans[j] = 0;\n    }\n    return false;\n  }\n  \n  public int[] constructDistancedSequence(int n) {    \n    int[] ans = new int[n * 2 - 1];\n    dfs(ans, n, 0, 0);\n    return 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 constructDistancedSequence(self, n: int) -> List[int]:\n    l = n * 2 - 1\n    ans = [0] * l\n    def dfs(i, s):\n      if i == l: return True\n      if ans[i]: return dfs(i + 1, s)\n      for d in range(n, 0, -1):\n        j = i + (0 if d == 1 else d)\n        if s & (1 << d) or j >= l or ans[j]: continue\n        ans[i] = ans[j] = d\n        if dfs(i + 1, s | (1 << d)): return True\n        ans[i] = ans[j] = 0\n      return False\n    dfs(0, 0)\n    return ans\n\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, find a sequence that satisfies all of the following: The integer&nbsp;1&nbsp;occurs once in the sequence. Each integer between&nbsp;2&nbsp;and&nbsp;n&nbsp;occurs twice in the sequence.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42],"class_list":["post-7950","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7950","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=7950"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7950\/revisions"}],"predecessor-version":[{"id":7959,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7950\/revisions\/7959"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}