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的更多相关文章

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

  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. dev GridControl显示标题

    gridview:ShowViewCaption = TrueViewCation = "32435354354"

  2. os.path.dirname( __ file __ ) 2018/6/2

    os.path.dirname( __ file __ ) 2018/6/2 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py import os #该文件所在位 ...

  3. centos7 yum 安装 mysql

    CentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1 ...

  4. jqgrid 自动换行

    <style>.ui-jqgrid tr.jqgrow td { white-space: normal !important; height: auto; vertical-align: ...

  5. Perl 变量:哈希变量

    Perl 哈希变量哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}. 1.创建哈希创建哈希可以通过以下两种方式: 1.为每个 key ...

  6. c# winform 解决PictureBox 无法打印全部图片的问题

    一.   问题描述 在页面使用PictureBox 加载资料图片后,点击“打印”,只能打印图片首页,较大图片则无法全部打印. 二.   原因分析 PictureBox中打印图片时没有设置继续打印相关属 ...

  7. [cogs2638]数列操作ψ(双标记线段树)

    题目大意:给定一个数列a,你需要支持的操作:区间and,区间or,询问区间最大值 解题关键: 1.双标记线段树,注意优先级(超时) 当涉及多重标记时,定义出标记的优先级,修改操作时用优先级高(先下放) ...

  8. 规范抢先看!微信小程序的官方设计指南和建议

    基于微信小程序轻快的特点,我们(微信官方)拟定了小程序界面设计指南和建议. 设计指南建立在充分尊重用户知情权与操作权的基础之上.旨在微信生态体系内,建立友好.高效.一致的用户体验,同时最大程度适应和支 ...

  9. js 获取ur参数 只要问号后面的那段传进url

    //获取url中的参数 function getUrlParam (pName, win) { var sUrl; if (typeof (win) == 'string') { sUrl = win ...

  10. 导入excel精华版

    //须引入 NPOI, NPOI.OOXML, NPOI.Openxml4Net, NPOI.OpenxmlFormats等程序集 自己去下载吧 NPOI组件很好用不可能下不到自己去吧,通常去百度网盘 ...