【LeetCode】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛
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 <= 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)的更多相关文章
- [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 ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- LeetCode 896 Monotonic Array 解题报告
题目要求 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- GlimmerHMM指南
GlimmerHMM指南 官方用户手册 GlimmerHMM是一种De novo的新基因预测软件. 新基因发现基于Generalized Hidden Markov Model (GHMM). Gli ...
- JavaBean内省与BeanInfo
Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore", ...
- HDFS04 HDFS的读写流程
HDFS的读写流程(面试重点) 目录 HDFS的读写流程(面试重点) HDFS写数据流程 网络拓扑-节点距离计算 机架感知(副本存储节点的选择) HDFS的读数据流程 HDFS写数据流程 客服端把D: ...
- Gradle插件详解
参考[1]Gradle 插件 [2]修改 Gradle 插件(Plugins)的下载地址(repositories)
- navigationItem的leftBarButtonItem和rightBarButtonItem隐藏
- (void)showEdit { if (不符合显示条件) { self.navigationItem.rightBarButtonItem.customView.hidden = YES; // ...
- linux 加密安全之AWK
密钥 密钥一般是一串字符串或数字,在加密或者解密时传递给加密或者解密算法,以使算法能够正确对明文加密或密文解密. 加密算法 已知的加密算法有对称和非对称加密,也就是说你想进行加解密操作的时候需要具备密 ...
- @PropertySource配置的用法
功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和@ConfigurationProperties 使用. @Proper ...
- 【Spring Framework】Spring入门教程(二)基于xml配置对象容器
基于xml配置对象容器--xml 标签说明 alias标签 作用:为已配置的bean设置别名 --applicationContext.xml配置文件 <?xml version="1 ...
- 【Linux】【Services】【Package】rpm
CentOS系统上rpm命令管理程序包: 安装.升级.卸载.查询和校验.数据库维护 rpm命令:rpm [OPTIONS] [PACKAGE_F ...
- 【Linux】【Basis】Grub
GRUB(Boot Loader): 1. grub: GRand Unified Bootloader grub 0.x: grub legacy grub 1.x: grub2 2. gr ...