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 ...
随机推荐
- vs2015 产品密钥
一.破解秘钥 企业版 HM6NR-QXX7C-DFW2Y-8B82K-WTYJV 专业版 HMGNV-WCYXV-X7G9W-YCX63-B98R2 二.破解步骤 1.安装vs2015 2 ...
- x多进程
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...
- Cracking The Coding Interview 2.5
这题的思想来自于http://hawstein.com/posts/2.5.html,重新实现了一下 用hash来记录循环的起点 //Given a circular linked list, imp ...
- Docker常用命令学习
sudo service docker startsudo docker run hello-worlddocker stats --helpdocker run -d -P training/web ...
- ASCII码,utf-8
ASCII:0-127表示英文,128-255每个国家编码不一样,汉字要使用两个字节,为了和0-127区别,首位都要是1,uriEncode就是把字符转换成ASCII码. utf-8,一个字节的,和a ...
- 虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件
虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件 Win 10 vmware12 无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件. ...
- C代码与C++代码之间的相互调用
1.showCpp.cpp #include <iostream> using namespace std; // 声明为C语言可以调用的函数 extern "C" v ...
- python selenium爬取QQ空间方法
from selenium import webdriver import time # 打开浏览器 dr = webdriver.Chrome() # 打开某个网址 dr.get('https:// ...
- 2019-03-05-day004-列表操作
01 昨日内容回顾 bool int str 三者转换:pass int 二进制与十进制之间的转换: 二进制 -------> 十进制 0000 0100 1*2**2 str: msg = ' ...
- Python 验证进程之间是空间隔离的
from multiprocessing import Process num = 100 def f1(): global num num = 3 print("子进程中的num" ...