返回介绍

solution / 1700-1799 / 1718.Construct the Lexicographically Largest Valid Sequence / README_EN

发布于 2024-06-17 01:03:15 字数 5891 浏览 0 评论 0 收藏 0

1718. Construct the Lexicographically Largest Valid Sequence

中文文档

Description

Given an integer n, find a sequence that satisfies all of the following:

  • The integer 1 occurs once in the sequence.
  • Each integer between 2 and n occurs twice in the sequence.
  • For every integer i between 2 and n, the distance between the two occurrences of i is exactly i.

The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of their indices, |j - i|.

Return _the lexicographically largest sequence__. It is guaranteed that under the given constraints, there is always a solution. _

A sequence a is lexicographically larger than a sequence b (of the same length) if in the first position where a and b differ, sequence a has a number greater than the corresponding number in b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they differ is at the third number, and 9 is greater than 5.

 

Example 1:

Input: n = 3
Output: [3,1,2,3,2]
Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.

Example 2:

Input: n = 5
Output: [5,3,1,4,3,5,2,4,2]

 

Constraints:

  • 1 <= n <= 20

Solutions

Solution 1

class Solution:
  def constructDistancedSequence(self, n: int) -> List[int]:
    def dfs(u):
      if u == n * 2:
        return True
      if path[u]:
        return dfs(u + 1)
      for i in range(n, 1, -1):
        if cnt[i] and u + i < n * 2 and path[u + i] == 0:
          cnt[i] = 0
          path[u] = path[u + i] = i
          if dfs(u + 1):
            return True
          path[u] = path[u + i] = 0
          cnt[i] = 2
      if cnt[1]:
        cnt[1], path[u] = 0, 1
        if dfs(u + 1):
          return True
        path[u], cnt[1] = 0, 1
      return False

    path = [0] * (n * 2)
    cnt = [2] * (n * 2)
    cnt[1] = 1
    dfs(1)
    return path[1:]
class Solution {
  private int[] path;
  private int[] cnt;
  private int n;

  public int[] constructDistancedSequence(int n) {
    this.n = n;
    path = new int[n * 2];
    cnt = new int[n * 2];
    Arrays.fill(cnt, 2);
    cnt[1] = 1;
    dfs(1);
    int[] ans = new int[n * 2 - 1];
    for (int i = 0; i < ans.length; ++i) {
      ans[i] = path[i + 1];
    }
    return ans;
  }

  private boolean dfs(int u) {
    if (u == n * 2) {
      return true;
    }
    if (path[u] > 0) {
      return dfs(u + 1);
    }
    for (int i = n; i > 1; --i) {
      if (cnt[i] > 0 && u + i < n * 2 && path[u + i] == 0) {
        cnt[i] = 0;
        path[u] = i;
        path[u + i] = i;
        if (dfs(u + 1)) {
          return true;
        }
        cnt[i] = 2;
        path[u] = 0;
        path[u + i] = 0;
      }
    }
    if (cnt[1] > 0) {
      path[u] = 1;
      cnt[1] = 0;
      if (dfs(u + 1)) {
        return true;
      }
      cnt[1] = 1;
      path[u] = 0;
    }
    return false;
  }
}
class Solution {
public:
  int n;
  vector<int> cnt, path;

  vector<int> constructDistancedSequence(int _n) {
    n = _n;
    cnt.resize(n * 2, 2);
    path.resize(n * 2);
    cnt[1] = 1;
    dfs(1);
    vector<int> ans;
    for (int i = 1; i < n * 2; ++i) ans.push_back(path[i]);
    return ans;
  }

  bool dfs(int u) {
    if (u == n * 2) return 1;
    if (path[u]) return dfs(u + 1);
    for (int i = n; i > 1; --i) {
      if (cnt[i] && u + i < n * 2 && !path[u + i]) {
        path[u] = path[u + i] = i;
        cnt[i] = 0;
        if (dfs(u + 1)) return 1;
        cnt[i] = 2;
        path[u] = path[u + i] = 0;
      }
    }
    if (cnt[1]) {
      path[u] = 1;
      cnt[1] = 0;
      if (dfs(u + 1)) return 1;
      cnt[1] = 1;
      path[u] = 0;
    }
    return 0;
  }
};
func constructDistancedSequence(n int) []int {
  path := make([]int, n*2)
  cnt := make([]int, n*2)
  for i := range cnt {
    cnt[i] = 2
  }
  cnt[1] = 1
  var dfs func(u int) bool
  dfs = func(u int) bool {
    if u == n*2 {
      return true
    }
    if path[u] > 0 {
      return dfs(u + 1)
    }
    for i := n; i > 1; i-- {
      if cnt[i] > 0 && u+i < n*2 && path[u+i] == 0 {
        cnt[i] = 0
        path[u], path[u+i] = i, i
        if dfs(u + 1) {
          return true
        }
        cnt[i] = 2
        path[u], path[u+i] = 0, 0
      }
    }
    if cnt[1] > 0 {
      cnt[1] = 0
      path[u] = 1
      if dfs(u + 1) {
        return true
      }
      cnt[1] = 1
      path[u] = 0
    }
    return false
  }
  dfs(1)
  return path[1:]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文