博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
932. Beautiful Array(python+cpp)
阅读量:3701 次
发布时间:2019-05-21

本文共 2248 字,大约阅读时间需要 7 分钟。

题目:

HintsSubmissionsDiscussSolution 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

解释:

尝试使用分治法来做,所以把数组分为left和right两块:
一种方法是分·[1, N / 2][N / 2 + 1, N],但是在merge的时候会出现问题。
另一种方法是分为奇数块odd和偶数块even,也就是奇数块odd在左边,偶数快even在右边
所以没有k能够使得 A[k] * 2 = odd + even

Disscuss区的作者遍历了N=5d的所有的排列:

找到了20 个beautiful array ,其中只有4个不满足 odd + even 的模式:
[2, 1, 4, 5, 3]
[3, 1, 2, 5, 4]
[3, 5, 4, 1, 2]
[4, 5, 2, 1, 3]

beautiful array满足的条件是:

不存在 i < k < j,
使得 A[k] * 2 = A[i] + A[j]

对beautiful array 进行下列3种变换,可以得到新的beautiful array

  1. 删除

    很好证明

  2. 加法

    A[k] * 2 != A[i] + A[j],
    (A[k] + x) * 2 = A[k] * 2 + 2x != A[i] + A[j] + 2x = (A[i] + x) + (A[j] + x)
    E.g: [1,3,2] + 1 = [2,4,3].

  3. 乘法

    A[k] * 2 != A[i] + A[j],
    (A[k] * x) * 2 = A[k] * 2 * x != (A[i] + A[j]) * x = (A[i] * x) + (A[j] * x)

E.g: [1,3,2] * 2 = [2,6,4]

根据上述三个法则,我们可以很方便地构造 beautiful array。

假设我们有一个长度为Nbeautiful array A
A1 = A * 2 - 1是一个只包含 1N * 2 -1中的奇数的 beautiful array
A2 = A * 2是一个只包含 2 到 N * 2中的偶数的 beautiful array
B = A1 + A2 是一个长度为N * 2的 beautiful array

为什么A1+A2是 beautiful array?

因为,首先A是beautiful array,所以A1A2都分别是beautiful array,也就是说,在A1A2的内部,是满足约束条件的,现在就是要考虑有没有一种可能,A[i]A1中,A[j]A2中,使得 A[k] * 2 = A[i] + A[j]
很显然,这种情况是不存在的,因为A[i]A1中是奇数,A[j]A2中是偶数,他们俩相加一定是奇数,所以不可能是哪个整数的2倍。

E.g:

A = [2, 1, 4, 5, 3]
A1 = [3, 1, 7, 9, 5]
A2 = [4, 2, 8, 10, 6]
B = A1 + A2 = [3, 1, 7, 9, 5, 4, 2, 8, 10, 6]

然后,分治法需要用递归或者循环来实现。

python代码:

class Solution:    def beautifulArray(self, N):        """        :type N: int        :rtype: List[int]        """        res=[1]        while len(res)

c++代码:

class Solution {
public: vector
beautifulArray(int N) {
vector
res={
1}; while (res.size()
tmp; for (auto i : res) if(2*i-1<=N) tmp.push_back(2*i-1); for (auto i : res) if(2*i<=N) tmp.push_back(2*i); res=tmp; } return res; }};

总结:

转载地址:http://nrlcn.baihongyu.com/

你可能感兴趣的文章
[by暴走的山交君]B树、B+树详解
查看>>
[剑指Offer系列] 53 - II. 0~n-1中缺失的数字
查看>>
广义表中 GetHead() 和 GetTail()
查看>>
面试问题: 三次握手和四次挥手的理解?
查看>>
Java中==和equals的区别<深度理解> (转载)
查看>>
关于==和equals的区别和联系,面试这么回答就可以(转载)
查看>>
漫画 | 什么是 HashMap?
查看>>
计算机网络面试题整理
查看>>
Java知识体系最强总结(2020版) 传送门
查看>>
idea创建vue项目(转载)
查看>>
关于Vue中main.js,App.vue,index.html之间关系进行总结
查看>>
vue 全屏背景图片 别看其他的了看我这篇就解决了!
查看>>
GET和POST两种基本请求方法的区别
查看>>
Vue+SpringBoot+Mybatis-plus前后端交互实现登录功能(踩了好多坑!)
查看>>
git 上传管理项目执行命令
查看>>
常见编程命名缩写
查看>>
警示:SpringBoot后端:自己写的方法没有错啊 为什么总是报错!!java.lang.NullPointerException: null
查看>>
查询数据库中的数据字段不显示 或者 报Unknown column ‘xxx‘ in ‘field list‘错解决办法(踩坑)
查看>>
使用Mybatis-Plus时,注入mapper提示Could not autowire. No beans of ‘xxxMapper‘ type found.
查看>>
拦截器 路径不生效踩坑
查看>>