LC 932. Beautiful Array
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,因此这个式子是成立的。对偶数也是相同的情况。
所以,我们有这样一个递归的解法。
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的更多相关文章
- [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 ...
- 932. 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- [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 ...
- 漂亮数组 Beautiful Array
2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...
- LeetCode - Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 北邮校赛 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 ...
随机推荐
- pycharm的快捷键以及快捷意义
ctrl+a 全选 ctrl+c 复制(默认复制整行) ctrl+v 粘贴 ctrl+x 剪切(默认复制整行) ctrl+f 搜索 ctrl+z 撤销 ctrl+shift+z 反撤销 ctrl+d ...
- Odoo的菜单项
用户界面的入口是菜单项,菜单项形成一个层级结构,最顶级项为应用,其下一级为每个应用的主菜单.还可以添加更深的子菜单.可操作菜单与窗口操作关联,它告诉客户端在点击了菜单项后应执行什么操作. 菜单项存储在 ...
- 【异常】lockfile.AlreadyLocked: ~/airflow/airflow-scheduler.pid is already locked
1 完整异常信息 File "/usr/bin/airflow", line 32, in <module> args.func(args) File "/u ...
- JAVA程序员成长路线图
https://www.cnblogs.com/godtrue/p/4283708.html
- 使用js打印时去除页眉页脚
写在前面 今天的开发遇到了使用window.print()功能进行当前页面打印的功能,因为页脚左边部分显示了url,这是不能存在的,已解决,写在这里. 正文 很多网上的方法都是不能用的,最后我找到一个 ...
- python函数:叠加装饰器、迭代器、自定义迭代器、生成式
一.叠加多个装饰器二.迭代器三.自定义迭代器四.xxx生成式 一.叠加多个装饰器 # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数 # 在加载完毕后,调用原函数其实就是在调用 ...
- Linux基础命令01
绝对路径:从“/”根目录下开始 常用的一些命令选项 ls ls :使用方式 ls {空格}选项 {空格}参数 (全写为list)(等同于dir) 列出当前目录下所有的文件,包括隐藏文件 ls -a ...
- angular打印功能实现方式
目前主流的前端打印方式有两种:一种是使用浏览器打印功能直接打印页面,另一种是调用本地控件实现.浏览器打印功能单一,不适用于复杂的业务表单,而打印控件可以设计打印模板,实现复杂表单的打印,十分适合复杂的 ...
- mac+django(1.8.2)+uwsgi+nginx 部署
一. uwsgi 安装 检验 配置uwsgi.ini 1. 安装 pip3 install uwsgi 2. 检验 方法一(uwsgi启动文件): test.py内容如下: def applicati ...
- 开启防火墙如何部署k8s
你可以不关闭防火墙,只需要开启这些端口就行了MASTER节点6443* Kubernetes API server 2379-2380 etcd server client API 10250 Kub ...