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
 
 

一道很好的构造题。自己没有想出来,看了晚上的解答,但是感觉大家写的都差不多,但没有说到点子上。

1. 首先,基本的想法是让所有的奇数放在一边,让所有的偶数放在另一边,这样能确保当以中间的数为K时,左右两边不会加起来有偶数出现。

2. 再考虑两边的情况,这个时候就不能用奇数和偶数的性质了,因为在这里所有的数要么都是奇数,要么都是偶数。

这个时候,需要这样考虑,奇数也是有顺序的,比如说,1,3,5,7,9 这样的奇数序列就是递增的,1是第1个奇数,3是第2个奇数,5是第3个

奇数等。如果我们不是对奇数进行排列了,而是对奇数的顺序进行再递归调用刚才的思想,是否能得到正确的解答呢?

这就要考虑一个问题,假设存在2k != x + y,那第k个奇数,第x个奇数,第y个奇数是否也有这样的性质?第k个偶数,第x个偶数,第y个偶数是否也有这样的性质?

很简单,2(2*k-1) - (2*x-1) - (2*y-1) = 4*k - 2*x - 2*y = 2(2*k - x - y) != 0,因此这个式子是成立的。对偶数也是相同的情况。

所以,我们有这样一个递归的解法。

Accepted

Runtime: 8 ms, faster than 70.97% of C++ online submissions for Beautiful Array.

class Solution {
public:
vector<int> beautifulArray(int N) {
if(N == ) return {};
else{
vector<int> ret;
int oddnum = (N+)/;
vector<int> oddpart = beautifulArray(oddnum);
for(auto x : oddpart) ret.push_back(x*-);
int evennum = (N)/;
vector<int> evenpart = beautifulArray(evennum);
for(auto x : evenpart) ret.push_back(x*);
return ret;
}
}
};

LC 932. 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. 932. Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  3. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  4. [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 ...

  5. 漂亮数组 Beautiful Array

    2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...

  6. LeetCode - Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  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. JAVA语言程序设计课后习题----第一单元解析(仅供参考)

    1 本题是水题,基本的输出语句 public class test { public static void main(String[] args) { // 相邻的两个 "" 要 ...

  2. 由于MTU设置不当导致的访问超时

    现象 工作中遇到一件怪事:搭建好服务器后(VPN服务器,创建了虚拟网卡),服务器和客户端之间响应正常且很稳定,客户端也能正常通过服务器访问外网.但是访问个别网站时可以打开文字,但是部分图片打不开(也不 ...

  3. SSH安装配置

    一.环境准备 二.SSH配置 1.root用户进入home目录,确实有无隐藏文件夹 .ssh cd ~ ls -lrta 2.有,则跳过本步骤:没有,执行如下命令 ##根据提示输入当前用户密码 ssh ...

  4. 第一章·MySQL介绍及安装

    一.DBA工作内容及课程体系 二.MySQL课程体系介绍 三.DBA的职业素养 四.MySQL简介及安装 4.1 什么是数据? 数据(data)是事实或观察的结果,是对客观事物的逻辑归纳,是用于表示客 ...

  5. 使用 jenkins 为 nginx 增加上下文

    每次需要在Nginx增加上下文,都需要添加如下两段 ​ server.conf upstream serverdownloadPage { server 10.11.19.6:3023; } ​ ht ...

  6. Java#Spring框架下注解解析

    @Bean 定义Bean @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 @Configura ...

  7. Python:多进程。

    参考:https://www.liaoxuefeng.com/wiki/1016959663602400/1017628290184064 Python程序实现多进程(multiprocessing) ...

  8. HDU 6041 - I Curse Myself | 2017 Multi-University Training Contest 1

    和题解大致相同的思路 /* HDU 6041 - I Curse Myself [ 图论,找环,最大k和 ] | 2017 Multi-University Training Contest 1 题意 ...

  9. MessagePack Jackson 数据大小

    我们在使用 MessagePack 对 List 对象数据进行序列化的时候,发现序列化以后的二进制数组数据偏大的情况. 请注意,不是所有的 List 对象都会出现这种情况,这个根据你 List 对象中 ...

  10. 【CUDA 基础】3.5 展开循环

    title: [CUDA 基础]3.5 展开循环 categories: - CUDA - Freshman tags: - 展开归约 - 归约 - 模板函数 toc: true date: 2018 ...