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. POJ 1035 Spell checker 字符串 难度:0

    题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...

  2. 【转载】Qt中图像的显示与基本操作

    Qt可显示基本的图像类型,利用QImage.QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放.旋转). 参考:Qt中图像的显示与基本操作 - ykm0722的专栏 - ...

  3. Oracle shrink space

    一.开启表的行迁移 alter table table_name enable row movement; select 'alter table '||s.owner||'.'||s.table_n ...

  4. Saiku连接mysql数据库(二)

    Saiku连接Mysql数据库展示数据 参考链接:https://www.cnblogs.com/shirui/p/8573491.html 官方文档:https://media.readthedoc ...

  5. VSTO:使用C#开发Excel、Word【3】

    <Visual Studio Tools for Office: Using C# with Excel, Word, Outlook, and InfoPath >——By Eric C ...

  6. Spring-data-JPA详细介绍

    Spring-data-JPA学习: 1. https://blog.csdn.net/liujianwd/article/details/75411009 2.http://www.cnblogs. ...

  7. 多War包合并,jetty测试

    模块间的相互依赖引用配置在pom.xml中加入要依赖的模块即可<dependency>    <groupId>com.exayong</groupId>    & ...

  8. IBM MQ 集成CXF 发送JMS 消息

    0.POM依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  9. 关于对Access数据库的学习报告

    学习Access数据库的报告 一.前言 一开始我对access一窍不通,甚至不知道它是干什么的,后来经过网上资料的查阅对它略有了解.microsoft office access是由微软发布的关联式数 ...

  10. 8.Python爬虫实战一之爬取糗事百科段子

    大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...