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

这道题是用divide and conque 做的。参考自: https://blog.csdn.net/lwgkzl/article/details/83502656

因为2*A[k]是偶数,如果要求2*A[K]!=A[I]+A[J]那么可以构造位置在A[k]左边的全部放奇数,位置在A[k]右边的全部放偶数。这样就保证了对于K位置而言,这个性质是满足的。

也许你就有疑问了,1,3,5,6,2,4.这组序列中,虽然对于6位置而言,左边全是奇数,右边全是偶数,但是全是奇数的那一边明显不满足要求。怎么解决呢?

习惯从幼儿园开始就要养成,左边是奇数,右边是偶数的形式也得从N比较小的时候开始。这里有一个递归的思想。因为长度为N的序列中,奇数个数为(N+1)/2,偶数个数为N/2,所以Beautiful Array(N)的排列,可以由Beautiful Array(N/2)和Beautiful Array((N+1)/2)组成,为什么这么说呢?

假设N  = 7,那么Beautiful Array(N/2) = 1,,3,2(满足题目性质)  Beautiful Array((N+1)/2) = 1 3  2  4.(满足题目性质)

那么 2*Beautiful Array((N+1)/2)-1(奇数在前)连接  2*Beautiful Array(N/2) = 1  5  3  7 | 2  6  4.(逐个元素进行操作)

我们可以观察到,对于这个分隔符而言,左边是奇数,右边是偶数,所以在分隔符位置,无疑是满足题目要求的性质的。而且,左边的奇数是从满足题目性质的数组进行线性运算得出的,所以并不会影响各个位置数字间的性质,(如1,3,5. 2*3==1+5,同时乘2之后这个性质依然成立)。既然组成这个大数组的子数组Beautiful Array(N/2)和Beautiful Array((N+1)/2)都满足性质,那么乘法之后的数组也依然满足题目要求。所以大数组左半部分和右半部分都满足题目要求。而中间分隔位置也满足性质,所以这一整个大数组也满足题目性质了。所以我们只需要保证Beautiful Array(1)满足题目性质就可以递推出所有的Beautiful Array(N)了。很明显Beautiful Array(1)没有别的选择,只能满足性质。

class Solution {
public int[] beautifulArray(int N) {
int[] res = new int[N];
if(N == 1){
res[0] = 1;
return res;
}
else{
int[] arr1 = beautifulArray ((N+1)/2);
for(int i = 0; i<arr1.length; i++){
res[i] = 2*arr1[i] - 1;
}
int[] arr2 = beautifulArray(N/2); for(int i= arr1.length; i<arr1.length+arr2.length; i++){
res[i] = 2*arr2[i - arr1.length];
}
}
return res;
}
}

LeetCode - Beautiful Array的更多相关文章

  1. [LeetCode] 932. Beautiful Array 漂亮数组

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

  2. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  3. [Swift]LeetCode932. 漂亮数组 | Beautiful Array

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

  4. 932. Beautiful Array

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

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. 漂亮数组 Beautiful Array

    2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...

  7. Educational Codeforces Round 63 D. Beautiful Array

    D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 北邮校赛 I. Beautiful Array(DP)

    I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...

  9. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

随机推荐

  1. linux下 gogs的安装和web钩子

    linux系统下 gogs下载安装以及web钩子的使用 (1)下载gogs  官方网址:https://dl.gogs.io/ 选择合适的版本,解压后就可以使用了 启动gogs的命令:  ./gos ...

  2. MySQL - exists与in的用法

    [1]exists 对外表用loop逐条查询,每次查询都会查看exists的条件语句. 当 exists里的条件语句能够返回记录行时(无论记录行是多少,只要能返回),条件就为真 , 返回当前loop到 ...

  3. [Linux]Linux下开启snmp支持IPV4和IPV6

    SNMP简介 简单网络管理协议(SNMP),由一组网络管理的标准组成,包含一个应用层协议(application layer protocol).数据库模型(database schema)和一组资源 ...

  4. Centos7部署kubernetes API服务(四)

    1.准备软件包 [root@linux-node1 bin]# pwd /usr/local/src/kubernetes/server/bin [root@linux-node1 bin]# cp ...

  5. 《RECURRENT BATCH NORMALIZATION》

    原文链接 https://arxiv.org/pdf/1603.09025.pdf Covariate 协变量:在实验的设计中,协变量是一个独立变量(解释变量),不为实验者所操纵,但仍影响实验结果. ...

  6. leetcode第40题:组合总和II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  7. chromium ④

    chromium的源码非常大,选择合适的点入手能省不少力气.在 win7下编译chromium中我曾提到学习chromium源码的一个小工程test_shell,代码目录在src/webkit/too ...

  8. python列举django中间件的5个请求方法

    process——request:请求进来时,权限认证. process——view:路由匹配之后,能够得到试图的试图函数. process——exceptions:异常是执行. process——t ...

  9. python 正则匹配时间格式转换方法

    import re from datetime import datetime a = '2018年8月9日 10:10' s = re.findall('\d+',a) print(s) d = ' ...

  10. 标准I/O读写文件

    一.函数原型 1.FILE *fopen(const char *path, const char *mode); path:要打开文件路径及文件名: mode:  r 打开只读文件,该文件必须存在. ...