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:

  1. Input: 4
  2. Output: [2,1,4,3]

Example 2:

  1. Input: 5
  2. Output: [3,1,2,5,4]

Note:

  • 1 <= N <= 1000

Approach #1: divide and conquer. [C++]

  1. class Solution {
  2. public:
  3. vector<int> beautifulArray(int N) {
  4. vector<int> ans;
  5. ans.push_back(1);
  6. while (ans.size() < N) {
  7. vector<int> temp;
  8. for (int i : ans) if (i * 2 - 1 <= N) temp.push_back(i*2-1);
  9. for (int i : ans) if (i * 2 <= N) temp.push_back(i*2);
  10. ans = temp;
  11. }
  12. return ans;
  13. }
  14. };

  

Approach #2: [Python]

  1. class Solution(object):
  2. def beautifulArray(self, N):
  3. """
  4. :type N: int
  5. :rtype: List[int]
  6. """
  7. res = [1]
  8. while len(res) < N:
  9. res = [i * 2 - 1 for i in res] + [i * 2 for i in res]
  10. 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. 【原】Coursera—Andrew Ng机器学习—Week 9 习题—异常检测

    [1]异常检测 [2]高斯分布 [3]高斯分布 [4] 异常检测 [5]特征选择 [6] [7]多变量高斯分布 Answer: ACD B 错误.需要矩阵Σ可逆,则要求m>n  测验1 Answ ...

  2. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...

  3. Linux实战教学笔记44:NoSQL数据库开篇之应用指南

    第1章 NoSQL数据库 1.1 NoSQL概述 自关系型数据库诞生40年以来,从理论产生发展到现实产品,例如:大家最常见的MySQL和Oracle,逐渐在数据库领域里上升到了霸主地位,形成每年高达数 ...

  4. JAVA中Colllection的基本功能

    Collection中的add方法: 代码: public static void main(String[] args) {        // TODO Auto-generated method ...

  5. ZPL语言说明文档

    ■格式命令(format commands) 以︿开始 用于设定标签格式与数据 多条格式指令按顺序执行 ■控制指令(control commands) 以~开始 迫使打印机立即执行某一个指令的操作 可 ...

  6. 采用AutoIt实现文件上传

    在非常规的上传界面中,AutoIt可以操作Windows资源管理器实现上传路径的输入. AutoIt中编辑以下脚本,需通过“AutoIt Windows Info”定位资源管理器路径输入位置信息及打开 ...

  7. java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)

    总结一些日期常用的代码,方便以后直接拿 <code> /** * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) * @param year * @param m ...

  8. Perl 学习笔记-输入输出

    1.读取标准输入<STDIN>(行输入操作=> 读取一行直到换行符) chomp($line = <STDIN>); # 读取一行并去掉最后的换行符(不会自动去掉) pr ...

  9. [GO]并行和并发的区别

    并行:指在同一时刻,有多条指令在多个处理器上同时执行 并发:指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行,使得在宏观上具有多个进程同时执行的效果,但在微观上并不是同时执行的,只有把时 ...

  10. Centos6 hadoop2.6.0安装笔记

    系统环境: linux:Centos6-64bit hadoop:hadoop2.6.0 jdk:1.6.45 集群方式安装 一台master,3台slave master 192.168.111.1 ...