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

这道题定义了一种漂亮数组,说的是在任意两个数字之间,不存在一个正好是这两个数之和的一半的数字,现在让返回长度是N的一个漂亮数组,注意这里长度是N的漂亮数组一定是由1到N之间的数字组成的,每个数字都会出现,而且一定存在这样的漂亮数组。博主刚开始时是没什么头绪的,想着总不会是要遍历所有的排列情况,然后对每个情况去验证是否是漂亮数组的吧,想想都觉得很不高效,于是就放弃挣扎,直接逛论坛了。不出意外,最高票的还是你李哥,居然提出了逆天的线性时间的解法,献上膝盖,怪不得有网友直接要 Venmo 号立马打钱,LOL~ 这道题给了提示说是要用分治法来做,但是怎么分是这道题的精髓,若只是普通的对半分,那么在 merge 的时候还是要验证是否是漂亮数组,麻烦!但若按奇偶来分的话,那就非常的叼了,因为奇数加偶数等于奇数,就不会是任何一个数字的2倍了。这就是奇偶分堆的好处,这时任意两个数字肯定不能分别从奇偶堆里取了,那可能你会问,奇数堆会不会有三个奇数打破这个规则呢?只要这个奇数堆是从一个漂亮数组按固定的规则变化而来的,就能保证一定也是漂亮数组,因为对于任意一个漂亮数组,若对每个数字都加上一个相同的数字,或者都乘上一个相同的数字,则一定还是漂亮数组,因为数字的之间的内在关系并没有改变。明白了上面这些,基本就可以解题了,假设此时已经有了一个长度为n的漂亮数组,如何将其扩大呢?可以将其中每个数字都乘以2并加1,就都会变成奇数,并且这个奇数数组还是漂亮的,然后再将每个数字都乘以2,那么都会变成偶数,并且这个偶数数组还是漂亮的,两个数组拼接起来,就会得到一个长度为 2n 的漂亮数组。就是这种思路,可以从1开始,1本身就是一个漂亮数组,然后将其扩大,注意这里要卡一个N,不能让扩大的数组长度超过N,只要在变为奇数和偶数时加个判定就行了,将不大于N的数组加入到新的数组中,参见代码如下:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/932

参考资料:

https://leetcode.com/problems/beautiful-array/

https://leetcode.com/problems/beautiful-array/discuss/186679/Odd-%2B-Even-Pattern-O(N)

https://leetcode.com/problems/beautiful-array/discuss/187669/Share-my-O(NlogN)-C%2B%2B-solution-with-proof-and-explanation

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 932. Beautiful Array 漂亮数组的更多相关文章

  1. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  2. 932. Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  3. LC 932. Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  4. [LeetCode] 896. Monotonic Array 单调数组

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  5. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  6. [Leetcode] merge sorted array 合并数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  7. [Swift]LeetCode932. 漂亮数组 | Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  8. 漂亮数组 Beautiful Array

    2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...

  9. LeetCode - Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

随机推荐

  1. iOS沙盒目录简介、沙盒路径获取

    一.沙盒的意义 出于安全的考虑,iOS系统的沙盒机制规定每个应用只能访问当前沙盒目录下面的文件.但是对于一些用户级别的数据,考虑到很多软件都需要使用其中的数据,用户可以通过对当前的软件授权,让当前的应 ...

  2. 一定要熟练地使用常用的Foundation服务

    关于本文:作为一名iOS软件工程师,熟练的使用系统提供的强大的Foundation服务是必备的职业素养. NSString.NSMutableString NSArray.NSMutableStrin ...

  3. CSP-201609-4 交通规划

    问题描述 G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统. 建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路. ...

  4. shell脚本中执行sql脚本(mysql为例)

    1.sql脚本(t.sql) insert into test.t value ("LH",88); 2.shell脚本(a.sh     为方便说明,a.sh与t.sql在同一目 ...

  5. mybatis官方中文文档

    http://www.mybatis.org/mybatis-3/zh/index.html

  6. mui搜索框在ios平台上点击多次才弹出键盘的解决方法

    今天使用Hbuilder调试手机端时,发现搜索框在安卓系统下,点击一次就可以弹出键盘 但是在iso下非常的不规律,要点击多次 代码实现如下: <div class="mui-input ...

  7. Day2-G-Sticks-POJ1011

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  8. CAS实现单点登录(SSO)经典完整教程

    转自 http://blog.csdn.net/small_love/article/details/6664831 一.简介 1.cas是有耶鲁大学研发的单点登录服务器 2.本教材所用环境 Tomc ...

  9. 使用自己定义的DIV的滚动条

    基本思路: 让DIV浮动起来,利用postion:fixed/absolute,设定height:100% var $card=$("#cardDetail");      $ca ...

  10. win10安装mysql过程&&链接过程&&备份和导入数据&&grant命令

    win10安装mysql过程&&链接过程&&备份和导入数据&&grant命令   一 .安装 一开始在mysql官网(https://www.mysql ...