LeetCode - 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
这道题是用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的更多相关文章
- [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 ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- [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 ...
- 932. Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 漂亮数组 Beautiful Array
2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 北邮校赛 I. Beautiful Array(DP)
I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...
- [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 ...
随机推荐
- bzoj1692
题解: 二分最近的不相同 然后hash判断是否相同 然后贪心 代码: #include<bits/stdc++.h> using namespace std; #define ull un ...
- TCP拥塞控制和流量控制
TCP 的流量控制与拥塞控制可以说是一体的.流量控制是通过滑动窗口实现的,拥塞避免主要包含以下2个内容: (1)慢开始,拥塞避免 (2)快重传,快恢复 1.流量控制——滑动窗口 TCP采用大小可变 ...
- TortoiseGit 的下载与安装
一.下载 访问https://tortoisegit.org/ 二.安装 然后就next,install 配置参考:2. TortoiseGit安装与配置
- JAVA基本类型和包装类
JAVA的包装类 Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表 ...
- Oracle特殊字符转义:&和'
Oracle特殊字符转义:&和' 我们在SQL*PLUS下执行 SQL show all命令时,可以发现一个参数:define & (hex 26),如下所示 concat . ...
- TensorFlow随机值函数:tf.random_uniform
tf.random_uniform 函数 random_uniform( shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name ...
- synchronized(九)
在Java中是有常量池缓存的功能的,就是说如果我先声明了一个String str1 = “a”; 再声明一个一样的字符串的时候,取值是从原地址去取的,也就是说是同一个对象.这也就导致了在锁字符串对象的 ...
- mysql 数据库关于增加用户权限的问题
一新建用户 ----------- 二首先修改权限必须在电脑cmd 中运行 开设的权限 主要就是 1 所有库的 *.* 2 所有的表 db.* 3所有的字段db.t1 ...
- python cookies提取——从字符串到字典(一行Python代码)
def extract_cookies(cookie): """从浏览器或者request headers中拿到cookie字符串,提取为字典格式的cookies&quo ...
- 【Python】多进程-4
#练习:用event事件控制进程执行顺序,下面例子中,主进程main函数在创建了子进程之后,依然会往下执行,所以会出现主进程先打印出来的情况 import multiprocessing import ...