漂亮数组 Beautiful Array
2019-04-06 16:09:56
问题描述:

问题求解:
本题还是挺有难度的,主要是要考虑好如何去进行构造。
首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶,否则它们的和必为奇数,显然等式不成立。
那么如果我们将N的数组分成两个部分,一部分全奇数,一部分全偶数,并且这两个部分是Beautiful Array,那么将它们concat一下,得到的也是Beautiful Array。
那么如何得到这两个部分呢?
这就需要一些数学的证明了,很容易证明得到漂亮数组满足以下的两个性质。
- 同乘一个数依然是漂亮数组
- 同加一个数依然是漂亮数组
得到这两个性质后就可以进行构造了,从{1}开始,每次得到{A * 2 - 1}和{A * 2},显然这两个数组是漂亮数组,并且分别是奇数和偶数数组,将它们concat之后就会得到长度更长的数组,这样就可以构造出来结果需要的数组。
public int[] beautifulArray(int N) {
List<Integer> res = new ArrayList<>();
res.add(1);
while (res.size() < N) {
List<Integer> tmp = new ArrayList<>();
for (int i : res) if (i * 2 - 1 <= N) tmp.add(i * 2 - 1);
for (int i : res) if (i * 2 <= N) tmp.add(i * 2);
res = tmp;
}
return res.stream().mapToInt(i -> i).toArray();
}
漂亮数组 Beautiful Array的更多相关文章
- [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 ...
- LeetCode - 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 漂亮数组
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- 北邮校赛 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 ...
- 【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别
OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...
- 后缀数组(suffix array)
参考: Suffix array - Wiki 后缀数组(suffix array)详解 6.3 Suffix Arrays - 算法红宝书 Suffix Array 后缀数组 基本概念 应用:字 ...
- 数组(Array),二维数组,三维数组
数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...
随机推荐
- Oracle 10.2.0.5升级至11.2.0.4
参照MOS 官方文档Complete Checklist for Manual Upgrade to Oracle Database 11gR2 (11.2) (Doc ID 837570.1)一.升 ...
- 计算机网络网络层的IP地址划分及子码
现在在网络层,即就是TCP/IP协议里的网际互联层,最流行IP协议的就是IPV4.其中IP地址的格式是由32位二进制数字表示的,通常为了人们阅读习惯,将其转换成点分十进制来表示,如:192.168.1 ...
- UEP-添加
添加数据时候自动更新时间,注意添加数据时,要设置最大主键 时间的工具类:CommonUtil 人的工具类: ContextUtil ScmCompanyAccount scmCompanyAccoun ...
- CentOS 7 实现zabbix agent 自动添加,并链接到指定的模版
如果添加的agent端数量较少时 , 手动添加还是可以的 , 如果数量较多 , 那么zabbix-server 的Discovery自动发现功能便派上了用场 首先所需要加入的服务器zabbix-age ...
- work-7.2
安装ubuntu,jdk ,git,maven,Intellij. 配置GIT时,需要将在本地生成的公钥粘贴到服务端. 先占个座,具体过程待补充. -------------------------- ...
- android 开发设计模式---Builder模式
我们通过一个例子来引出Builder模式.假设有一个Person类,我们通过该Person类来构建一大批人,这个Person类里有很多属性,最常见的比如name,age,weight,height等等 ...
- activeMQ (一)
1.安装activeMQ,直接解压,启动2.访问localhost:8161/admin/ 用户名密码都是admin 3.点对点消息传递域,每个消息只能一个消费者,消息的生产者与消费者之间没有 时间上 ...
- Tomcat部署-端口、项目名称
端口: 将Connector的8080端口换成,电脑网页服务的80端口 项目名称 直接再 Host 中添加 <Context path="/" docBase="s ...
- padding和margin
padding (内边距) 语法: (1)padding-left:10px; 左内边距 (2)padding-right:10px; 右内边距 (3)padding-top:10px; 上内边距 ( ...
- oracle传输表空间相关
1.convert tablespaceconvert tablespace源端库执行:convert tablespace 'TPS_DATA' to platform 'AIX-Based Sys ...