漂亮数组 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] ; ...
随机推荐
- 深入理解MVC原理
SpringMVC的工作原理图: SpringMVC流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherServlet收到请求调用HandlerMa ...
- Fiddler抓包【6】_Fiddler Script
1.安装SyntaxView插件 使用Fiddler Script前需要安装SyntaxView插件: 方式1:Inspectors tab--->Get SyntaxView tab---&g ...
- 利用Oracle Database Resource Manager实现UNDO表空间的quota
1.查出当前使用的是哪个resource plan select * from GV$RSRC_PLAN 2.创建pending area begin dbms_resource_manager.c ...
- WebApi接口访问异常问题。尝试创建“testController”类型的控制器时出错。请确保控制器具有无参数公共构造函数
本来运行的好好的webAPI 接口突然报了个 :“尝试创建“testController”类型的控制器时出错.请确保控制器具有无参数公共构造函数” 错误.耗了半宿最终解决了, 原因: api控制器中引 ...
- kendo upload必填验证
@using Kendo.Mvc.UI @using StudentManage.Common.Helper @model StudentManage.Models.Home.ImportDataFr ...
- 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)
将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertion ...
- Xamarin.Forms踩坑集锦(持续更新)
1.ImageButton控件 问题:ImageButton在切换图片的时候,图片大小会改变. Github Issue:ImageButton changes image size · Issue ...
- CCF CSP 201612-1 中间数
题目链接:http://118.190.20.162/view.page?gpid=T52 问题描述 试题编号: 201612-1 试题名称: 中间数 时间限制: 1.0s 内存限制: 256.0MB ...
- 对oracle数据库的数据迁移
导入:expdp 用户名/密码@ip/实例名 DUMPFILE=dump.dump EXCLUDE=TABLE:\"IN (\'表1\',\'表2\')\" 导出:impdp 用户 ...
- springboot整合mybatis(使用MyBatis Generator)
引入依赖 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> ...