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. mysql的锁机制,以及乐观锁,悲观锁,以及热点账户余额问题

    mysql的简单锁机制. myisam 1.只支持表级锁,所以经常更新的表结构不适宜用. 2.select也会产生锁表 innodb 1.支持事务,行级锁,表级锁,执行行级锁的前提是sql语句的索引有 ...

  2. malloc/calloc/realloc/alloca内存分配函数

    calloc(), malloc(), realloc(), free(),alloca() 内存区域可以分为栈.堆.静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取 ...

  3. odoo字段属性

    以下为可用的非关联字段类型以及其对应的位置参数: Char(string)是一个单行文本,唯一位置参数是string字段标签. Text(string)是一个多行文本,唯一位置参数是string字段标 ...

  4. VToRay C-S config

    Server config: { "inbounds": [{ "port": 20000, //Server Listening Port "pro ...

  5. CentOS下安装软件

    CentOS下安装软件,要看下载的软件安装包的后缀名是什么,一般为了方便安装,推荐下载以 rpm 结尾的软件包. 比如以下截图,有多种下载方式,推荐下载圈起来的链接. rpm包安装方式步骤: 找到相应 ...

  6. linux 重定向类型 超级块 i节点

    超级块:定义文件系统的元数据(总大小.块大小.空闲.......):在格式化的时候确定 查看超级块信息:tune2fs  -l   目录 i节点:定义文件的元数据(名称.大小.存放位置.权限.修改时间 ...

  7. Jmeter (一) 安装

    https://blog.csdn.net/ls1792304830/article/details/52718177 一.准备工具 1.Windows 操作系统 2.jmeter 3.JDk 依赖环 ...

  8. Tunnel Warfare HDU - 1540 (线段树处理连续区间问题)

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  9. TCP/IP——内网IP

    版权声明:本文系博主原创文章,转载或引用请注明出处. 1)背景 REC 1918留出了3块IP地址空间(1个A类地址段,16个B类地址段,256个C类地址段)作为私有的内部使用的地址. 在这个范围内的 ...

  10. 服务器syns to listen sockets drop导致创建socket失败

    在一次测试执行过程中,发现服务器TCP发送队列较长,执行netstat -s | grep LISTEN,发现有SYNs包被丢弃,但是没有times the listen queue of a soc ...