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
Approach #1: divide and conquer. [C++]
class Solution {
public:
vector<int> beautifulArray(int N) {
vector<int> ans;
ans.push_back(1);
while (ans.size() < N) {
vector<int> temp;
for (int i : ans) if (i * 2 - 1 <= N) temp.push_back(i*2-1);
for (int i : ans) if (i * 2 <= N) temp.push_back(i*2);
ans = temp;
}
return ans;
}
};
Approach #2: [Python]
class Solution(object):
def beautifulArray(self, N):
"""
:type N: int
:rtype: List[int]
"""
res = [1]
while len(res) < N:
res = [i * 2 - 1 for i in res] + [i * 2 for i in res]
return [i for i in res if i <= N]
Analysis:
https://leetcode.com/problems/beautiful-array/discuss/186679/C%2B%2BJavaPython-Odd-%2B-Even-Pattern-O(N)
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 ...
- 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 ...
- 【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 ...
随机推荐
- 后端生成二维码 - C#生成二维码(QR)
最近在github上找到一个相对比较好的C#二维码生成类库.在这里和大家分享一下. github地址:https://github.com/codebude/QRCoder 把解决方案下载下来,编译生 ...
- 用vim生成一批递增ID
假设说要生成1000个以xxx开头的后面加数字的ID,比如xxx1到xxx1000.一般我们可以通过.csv去递增,然后替换,但是直接用vim也是可以达到这样的目的. 下面通过一个gif图演示这个过程 ...
- go_常量与枚举
package main import ( "fmt" "math" ) //常量的数值可以作为各种类型使用 func consts(){ const file ...
- 111. Minimum Depth of Binary Tree (Tree; DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- MySQL多表查询回顾
----------------------siwuxie095 MySQL 多表查询回顾 以客户和联系人为例(一对多) 1.内连接 /*内连接写法一*/ select * from t_custom ...
- Mask_rcnn openpose realsense
cd /home/luo/Desktop/MyFile/Mask_RCNN_Openpose_Realsense python realsense_mask_openpose_2019032601.p ...
- Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)
1.添加依赖架包: <dependency> <groupId>org.springframework.data</groupId> <artifactId& ...
- AM使用指南:如何在Managed Bean中获取AM实例?
AM是放置服务方法的地方,有时我们需要在Managed Bean中调用这些方法.要调用这些方法,首先要在Managed Bean中获取AM实例.这里要用到<ADF工具类:ADFUtil.java ...
- WebAPI如何返回json
public HttpResponseMessage PostUser(User user) { JavaScriptSerializer serializer = new JavaScriptSer ...
- Openssl ciphers命令
一.简介 ciphers指令是用来展示用于SSL加密算法的工具 二.语法 openssl ciphers [-v] [-ssl2] [-ssl3] [-tls1] [cipherlist] 选项 -v ...