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 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. 首先,基本的想法是让所有的奇数放在一边,让所有的偶数放在另一边,这样能确保当以中间的数为K时,左右两边不会加起来有偶数出现。
2. 再考虑两边的情况,这个时候就不能用奇数和偶数的性质了,因为在这里所有的数要么都是奇数,要么都是偶数。
这个时候,需要这样考虑,奇数也是有顺序的,比如说,1,3,5,7,9 这样的奇数序列就是递增的,1是第1个奇数,3是第2个奇数,5是第3个
奇数等。如果我们不是对奇数进行排列了,而是对奇数的顺序进行再递归调用刚才的思想,是否能得到正确的解答呢?
这就要考虑一个问题,假设存在2k != x + y,那第k个奇数,第x个奇数,第y个奇数是否也有这样的性质?第k个偶数,第x个偶数,第y个偶数是否也有这样的性质?
很简单,2(2*k-1) - (2*x-1) - (2*y-1) = 4*k - 2*x - 2*y = 2(2*k - x - y) != 0,因此这个式子是成立的。对偶数也是相同的情况。
所以,我们有这样一个递归的解法。
Runtime: 8 ms, faster than 70.97% of C++ online submissions for Beautiful Array.
class Solution {
public:
vector<int> beautifulArray(int N) {
if(N == ) return {};
else{
vector<int> ret;
int oddnum = (N+)/;
vector<int> oddpart = beautifulArray(oddnum);
for(auto x : oddpart) ret.push_back(x*-);
int evennum = (N)/;
vector<int> evenpart = beautifulArray(evennum);
for(auto x : evenpart) ret.push_back(x*);
return ret;
}
}
};
LC 932. Beautiful Array的更多相关文章
- [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 ...
- 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】932. Beautiful Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...
- [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 ...
- 漂亮数组 Beautiful Array
2019-04-06 16:09:56 问题描述: 问题求解: 本题还是挺有难度的,主要是要考虑好如何去进行构造. 首先考虑到2 * A[i] = A[j] + A[k],那么j,k就必须是同奇同偶, ...
- LeetCode - Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 北邮校赛 I. Beautiful Array(DP)
I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
随机推荐
- Java注解【四、自定义注解】
语法要求.元注解 元注解: Target-适用范围: Retention-类型:源码注解.编译时注解.运行时注解 Inherited-可继承(只能继承类上的注解,接口.类中的方法都不行) Docume ...
- 关于php 7.4编译安装
一个不错的文章 https://hqidi.com/150.html 贴个地址就好啦, 借鉴了一把,很良心
- Asp.Net Zero轻量级审核流设计
复杂的业务系统中往往会集成工作流或审核流,但有些轻量及的业务系统对这些功能的需求并不大,有的系统甚至只需要审核功能就够了.这里给大家介绍在Asp.Net Zero中通用轻量及审核流设计,功能具备审核权 ...
- Redis07-Redis单节点容量问题,twemproxy,predixy的使用
Redis单节点容量问题 一.单节点容量问题 我们在实际场景中,往往遇上一个单节点容量问题. 1.进行业务拆分,数据分类 2.到了数据不能拆分的时候,可以进行数据分片 进行哈希取模(影响分布式下的扩展 ...
- 通过maven命令将源代码编译成jar到本地仓库
图: 4.2.3 采用maven命令编译成jar安装到本地maven库 在路径框输入cmd,执行命令: mvn clean install 图: 图2 成功后可以看到jar包
- [APIO2010] 算法竞赛竞赛经典 巡逻
原题链接 题目描述 在一个地区有 n 个村庄,编号为1,2,-,n. 有 n-1 条道路连接着这些村庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到达其他任一个村庄. 每条道路的长度 ...
- Codeforces 1082 毛毛虫图构造&最大权闭合子图
A #include<bits/stdc++.h> using namespace std; typedef long long ll; , MAXM = ; //int to[MAXM ...
- django虚拟环境搭建
windows建立Django项目(建立虚拟环境,安装virtualenv,安装Django,创建项目) 目的:为每一个项目单独配置一个环境例如:项目一使用django1.10, 项目二使用djang ...
- windows10家庭版远程桌面连接报错:CredSSP加密oracle修正
转 原地址:https://www.cnblogs.com/lindajia/p/9021082.html Windows10远程桌面连接 报错信息 : 网上找到方法 但是奈何是 "Win1 ...
- web添加学生信息(首发web)
程序思路,先在JSP上画好页面,然后再创建一Servlet文件用于判断在网页上操作是否正确,还需要与数据库相连接,用DBUtile文件连接数据库,用Dao层来实现数据的增加,用Service来服务于D ...