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. 下拉菜单 Spinner 简单纯字符串版

    下拉菜单 Spinner 简单纯字符串版 public class MainActivity extends Activity implements AdapterView.OnItemSelecte ...

  2. K nearest neighbor cs229

    vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...

  3. 性能篇——函数调用结果的 LRU 缓存

    1. 应用场景: 多次调用同一函数 2. 普通写法: def say(name): print("hellow:%s"%name) now = datetime.datetime. ...

  4. Python学习 第一天(一)初始python

    1.python的前世今生 想要充分的了解一个人,无外乎首先充分了解他的过去和现在:咱们学习语言也是一样的套路 1.1 python的历史 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈp ...

  5. 实操 | 内存占用减少高达90%,还不用升级硬件?没错,这篇文章教你妙用Pandas轻松处理大规模数据

    注:Pandas(Python Data Analysis Library) 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.此外,Pandas 纳入了大量库和一些标准的数据模型 ...

  6. Mybatis关联查询(转载)

    原文地址: http://www.cnblogs.com/xiaolang8762400/p/7399892.html   mybatis 提供了高级的关联查询功能,可以很方便地将数据库获取的结果集映 ...

  7. pycharm图像不能显示,之前是可以显示的。显示一两次突然不显示了

    网上说是什么包问题的就说了.我遇到一个非常奇葩的问题 因为你的设置可能是这样 每次都在窗口右侧的工具栏那边显示.可能突然心情不佳就不显示了.然后你再把勾去掉即可.你要喜欢再点上也行.

  8. Linux/Ubantu 安装 idea

    wget 使用 wget url (这里的url就是你要下载idea的网站) 在idea官网中 找到 direct link 右键复制链接 在 linux 中 打开 终端命令窗口 (Ctrl +Alt ...

  9. MariaDB安装与使用

    下载地址:https://downloads.mariadb.org/ 下载相对应的电脑版本程序 等待下载完成...... 安装教程: 双击运行 设置数据库的密码 等待安装完成.. 这样就完成安装了. ...

  10. 小程序是单页面应用,有一个页面执行wx.showLoading(),其他页面也会显示

    my.js onLoad: function (options) { setTimeout(res=>{ wx.showLoading({ title: '10s后出现', }) }, ) }, ...