【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 ...
随机推荐
- cat的生产应用
web日志文件的合并 cat one.log two.log >all.log sort -k 4 all.log 按照第四列进行时间排序
- Macbookpro vim操作键说明
i → Insert 模式,按 ESC 回到 Normal 模式. x → 删当前光标所在的一个字符.:wq → 存盘 + 退出 (:w 存盘, :q 退出) (陈皓注::w 后可以跟文件名)dd → ...
- Mysql-单个left join 计算逻辑(一对多问题)
BUG背景: 我们有一个订单表 和 一个 物流表 它们通过 订单ID 进行一对一的关系绑定.但是由于物流表在保存订单信息的时候没有做判断该订单是否已经有物流信息,这就变成同一个订单id在物流表中存在多 ...
- Spring Cloud中五花八门的分布式组件我到底该怎么学
分布式架构的演进 在软件行业,一个应用服务随着功能越来越复杂,用户量越来越大,尤其是互联网行业流量爆发式的增长,导致我们需要不断的重构应用的结构来支撑庞大的用户量,最终从一个简单的系统主键演变成了一个 ...
- 日常Java 2021/10/29
Java Object类是所有类的父类,也就是说Java的所有类都继承了Object,子类可以使用Object的所有方法. Object类位于java.lang 包中,编译时会自动导入,我们创建一个类 ...
- Spark基础:(二)Spark RDD编程
1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...
- Spark(十六)【SparkStreaming基本使用】
目录 一. SparkStreaming简介 1. 相关术语 2. SparkStreaming概念 3. SparkStreaming架构 4. 背压机制 二. Dstream入门 1. WordC ...
- Vue相关,vue.nextTick
vue中有一个较为特殊的API,nextTick.根据官方文档的解释,它可以在DOM更新完毕之后执行一个回调,用法如下: // 修改数据 vm.msg = 'Hello' // DOM 还没有更新 V ...
- clickhouse安装数据导入及查询测试
官网 https://clickhouse.tech/ quick start ubantu wget https://repo.yandex.ru/clickhouse/deb/lts/main/c ...
- Oracle存储过程游标for循环怎么写
一.不带参数的游标for循环 首先编写存储过程的整体结构,如下: create or replace procedure test_proc is v_date date; --变量定义 begin ...