作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/beautiful-array/description/

题目描述

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. 1 <= N <= 1000

题目大意

给出从1到N的数组的一个排列,使得任意两个数字之间不存在他们的平均数。

解题方法

构造法

因为题目要求任意两个数的平均数不能在他们中间,如果一个数字左边都是奇数,右边都是偶数,那么肯定这个数字的二倍是偶数,肯定不会存在A[k] * 2 = A[i] + A[j]

若数组A满足上面的条件,那么很容易从线性关系中看出来,对于A中的每个元素做[2 * i for i in A]后者[2 * i - 1 for i in A]依然满足上面的条件。

所以我们从最简单的1开始推导,构造奇数+偶数拼接在一起成为新的数组,然后继续这个操作,就能使得得到的一直是满足条件的数组。最后当数组的长度满足条件就结束。因为结果数组的长度是2的整数次方,所以最后要把结果中小于等于N的留下来就行了。

时间复杂度是O(NlogN),空间复杂度是O(N).打败100%。

class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
res = [1]
while len(res) < N:
res = [2 * i - 1 for i in res] + [2 * i for i in res]
return [i for i in res if i <= N]

递归

可以把上面的方法改成递归写法。思想是类似的,只不过要注意的是因为N可能是偶数也可能是奇数,当是奇数的时候除以二的时候可能丢失了一个奇数,所以小于N的奇数个数是N / 2 + N % 2.

时间复杂度是O(NlogN),空间复杂度是O(NlogN).打败25%。

class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
if N == 1: return [1]
odd = [i * 2 - 1 for i in self.beautifulArray(N / 2 + N % 2)]
even = [i * 2 for i in self.beautifulArray(N / 2)]
return odd + even

相似题目

参考资料

推荐寒神的视频:https://www.youtube.com/watch?v=9L6bPGDfyqo&t=41s

日期

2018 年 10 月 30 日 —— 啊,十月过完了

【LeetCode】932. Beautiful Array 解题报告(Python)的更多相关文章

  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. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  3. LeetCode 896 Monotonic Array 解题报告

    题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...

  4. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  5. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. R语言与医学统计图形-【34】绘制统计表格

    表的绘制,主要是临床三线表. 1.tableone包 #install.packages('tableone') library(tableone) set.seed(2017) age <- ...

  2. MYSQL5.8-----3

    666666666666666666666666 如多带有通配符的,要使用一下格式 select * from user where usename like "%1\%" ESC ...

  3. C/C++ Qt StandardItemModel 数据模型应用

    QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...

  4. 前端4 — jQuery — 更新完毕

    1.下载jQuery 网址:Download jQuery | jQuery  最好下载最新版的,因为有什么bug问题,最新版的都会有,所以学技术就用最新版的,实际开发用的时候就要讲究了 2.开始用j ...

  5. OpenStack——云平台部署

    一.配置网络 准备:安装两台最小化的CentOS7.2的虚拟机,分别添加两张网卡,分别为仅主机模式和NAT模式,并且计算节点设置为4G运行内存,50G硬盘 1.控制节点--配置网络 控制节点第一个网卡 ...

  6. 扩展kmp 学习笔记

    学习了一下这个较为冷门的知识,由于从日报开始看起,还是比较绕的-- 首先定义 \(Z\) 函数表示后缀 \(i\) 与整个串的 \(lcp\) 长度 一个比较好的理解于实现方式是类似于 \(manac ...

  7. Spark(七)【RDD的持久化Cache和CheckPoint】

    RDD的持久化 1. RDD Cache缓存 ​ RDD通过Cache或者Persist方法将前面的计算结果缓存,默认情况下会把数据以缓存在JVM的堆内存中.但是并不是这两个方法被调用时立即缓存,而是 ...

  8. 『学了就忘』Linux启动引导与修复 — 69、启动引导程序(grub)

    目录 1.启动引导程序(Boot Loader)简介 2.启动引导程序grub的作用 3.启动引导程序grub的位置 4./grub目录中其他的文件简单介绍 提示: 简单地说,Boot Loader就 ...

  9. js 如何全部替代一个子串为另一个子串

    更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...

  10. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...