【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 ...
随机推荐
- W10: Warning: Changing a readonly file使用vi/vim报错问题解决
使用vi/vim编辑文件的时候出现W10: Warning: Changing a readonly file报错 解决方法: 一.强制保存退出 :wq! 二.ll 查询文件属主,使用属主赋予权限 c ...
- Excel-姓名列中同一个人汇总金额列,得出总金额
8.姓名列中同一个人求和金额列,得出总金额. 方法一: P2处公式=SUMPRODUCT(($M$2:$M$20=$M2)*($N$2:$N$20)) 解释函数: 引用:https://zhinan. ...
- 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...
- A Child's History of England.36
CHAPTER 11 ENGLAND UNDER MATILDA AND STEPHEN The King was no sooner dead than all the plans and sche ...
- windows Notepad++ 上配置 vs 编译器 , 编译并运行
windows 中 配置 vs编译器 在Linux下,Kris是倾向于在终端中使用gcc和g++来编译C/C++的,在Windows下相信很多人都是选择臃肿的Visual Studio,我亦不免如此. ...
- Gradle安装与配置
一.Gradle安装 1.Gradle安装 (1)先安装JDK/JRE (2)Gradle下载官网 Gradle官网 (3)解压安装包到想安装到的目录.如D:\java\gradle-5.2.1 (4 ...
- Hibernate 错误的问题
配了好几次的Hibernate,老是在create BeanFactory的时候fail.我是用MyEclipse自带的HIbernate,直接加进去的. private static final T ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- treeTable实现排序
/* * * TreeTable 0.1 - Client-side TreeTable Viewer! * @requires jQuery v1.3 * * Dual licensed under ...
- MyBatis一对多映射简单查询案例(嵌套结果)
一.案例描述 书本类别表和书本信息表,查询书本类别表中的某一记录,连带查询出所有该类别书本的信息. 二.数据库表格 书本类别表(booktypeid,booktypename) 书本信息表(booki ...