[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array

For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such that:

For every i < j, there is no k with i < k < j such that A[k] * 2 = A[i] + A[j].

Given N, return any beautiful array A.  (It is guaranteed that one exists.)

Example 1:

Input: 4
Output: [2,1,4,3]

Example 2:

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

Note:

  • 1 <= N <= 1000


对于某些固定的 N,如果数组 A 是整数 1, 2, ..., N 组成的排列,使得:

对于每个 i < j,都不存在 k 满足 i < k < j 使得 A[k] * 2 = A[i] + A[j]

那么数组 A 是漂亮数组。

给定 N,返回任意漂亮数组 A(保证存在一个)。

示例 1:

输入:4
输出:[2,1,4,3]

示例 2:

输入:5
输出:[3,1,2,5,4]

提示:

  • 1 <= N <= 1000


16ms

 1 class Solution {
 2     func beautifulArray(_ N: Int) -> [Int] {
 3         var a:[Int] = [Int](repeating: 0,count: N)
 4         dfs(1, 0, N, &a,0)
 5         for i in 0..<N
 6         {
 7             a[i] += 1
 8         }
 9         return a
10     }
11     //深度优先搜索。DFS即Depth First Search.
12     //对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次
13     func dfs(_ d:Int,_ m:Int,_ n: Int,_ a:inout [Int],_ off:Int) -> Int
14     {
15         var off = off
16         if m >= n {return off}
17         if m + d >= n
18         {
19             a[off] = m
20             off += 1
21             return off
22         }
23         for i in 0..<2
24         {
25             off = dfs(d*2, m+d*i, n, &a, off)
26         }
27         return off
28     }
29 }

原文地址:https://www.cnblogs.com/strengthen/p/9865478.html

时间: 2024-10-08 16:33:47

[Swift Weekly Contest 108]LeetCode932. 漂亮数组 | Beautiful Array的相关文章

漂亮数组 Beautiful Array

2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶,否则它们的和必为奇数,显然等式不成立. 那么如果我们将N的数组分成两个部分,一部分全奇数,一部分全偶数,并且这两个部分是Beautiful Array,那么将它们concat一下,得到的也是Beautiful Array. 那么如何得到这两个部分呢? 这就需要一些数学的证明了,很容易证明得到漂亮数组

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 108]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 111]LeetCode941. 有效的山脉数组 | Valid Mountain Array

Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i with 0 < i < A.length - 1 such that: A[0] < A[1] < ... A[i-1] < A[

[Swift Weekly Contest 114]LeetCode954. 二倍数对数组 | Array of Doubled Pairs

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2. Example 1: Input: [3,1,3,6] Output: false Example 2: Input: [2,1,2,6] Output:

[Swift Weekly Contest 127]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.  (We may choose the same index i multiple times.) Return the largest possible sum of

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 113]LeetCode952. 按公因数计算最大组件大小 | Largest Component Size by Common Factor

Given a non-empty array of unique positive integers A, consider the following graph: There are A.length nodes, labelled A[0] to A[A.length - 1]; There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.