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
Approach #1: divide and conquer. [C++]
class Solution {
public:
vector<int> beautifulArray(int N) {
vector<int> ans;
ans.push_back(1);
while (ans.size() < N) {
vector<int> temp;
for (int i : ans) if (i * 2 - 1 <= N) temp.push_back(i*2-1);
for (int i : ans) if (i * 2 <= N) temp.push_back(i*2);
ans = temp;
}
return ans;
}
};
Approach #2: [Python]
class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
res = [1]
while len(res) < N:
res = [i * 2 - 1 for i in res] + [i * 2 for i in res]
return [i for i in res if i <= N]
Analysis:
https://leetcode.com/problems/beautiful-array/discuss/186679/C%2B%2BJavaPython-Odd-%2B-Even-Pattern-O(N)
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 ...
- 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 ...
- 【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 ...
随机推荐
- dev GridControl显示标题
gridview:ShowViewCaption = TrueViewCation = "32435354354"
- docker plugin test
docker build -t docker-volume-drbd . id=$(docker create docker-volume-drbd true) docker export $id - ...
- jsp实现查询与展示功能
今天做了一天的这个功能,开始是数据库一直打不开,显示Class not found,一直调试,调试了一下午.原来是mysql的jar包,必须要放到WEB-INF这个目录下的lib文件夹中,然后bui ...
- Struts2分模块开发
-------------------siwuxie095 Struts2 分模块开发 在实际开发中,如果一个项目是团队开发的,也就是很多人开发的, 每个人都需要去修改 struts.xml,因为 s ...
- python2.7 跨文件全局变量的方法-乾颐堂
在使用Python编写的应用的过程中,有时会遇到多个文件之间传递同一个全局变量的情况. 文件1:globalvar.py 1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/e ...
- oracle sql 数结构表id降序
UPDATE BAS_ORGANIZATION_TYPE T1SET T1.PARENTID=(select rn from (SELECT id,rownum rn FROM BAS_ORGANIZ ...
- Smarty模板的引用
(1)include用法和php里的include差不多(2)smarty的include还具备自定义属性的功能例如 {include file="header.tpl" titl ...
- mvc中图片的保存和显示
图片保存到网站根目录(例如 D:\www\Web)下 Web 应用程序根目录运算符用“~”表示.注意不是网站根目录(网站根目录是指针对域名计算的) 即便是网站部署在其他网站下(例如IIS中,在A站下( ...
- CodeForces 342B Xenia and Spies (水题模拟,贪心)
题意:给定 n 个间谍,m个区间,一个 s,一个f,然后从 s开始传纸条,然后传到 f,然后在每个 t 时间在区间内的不能传,问你最少的时间传过去. 析:这个题,就模拟一下就好,贪心策略,能传就传,找 ...
- 大型Unity手游《英雄之刃-最后之战》源码分析
英雄之刃之最后一战是国内首款原创精品MOBA手游,是一款由前暴雪文案亲自操刀世界观,日韩专业团队打造美术场景,新加坡团队精心制作战斗音乐的旷世之作! 超快速的匹配对战.默契的团队协作给你带来意犹未尽的 ...