Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: 
Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.

If there are multiple answers, print any of them.

Example 1:

Input: n = 3, k = 1
Output: [1, 2, 3]
Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.

Example 2:

Input: n = 3, k = 2
Output: [1, 3, 2]
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.

Note:

  1. The n and k are in the range 1 <= k < n <= 104.

这道题虽然也叫优美排列,但是貌似跟之前那道Beautiful Arrangement的关系不太大。这道题给我们了一个数字n和一个数字k,让找出一种排列方式,使得1到n组成的数组中相邻两个数的差的绝对值正好有k种。给了k和n的关系为k<n。那么我们首先来考虑,是否这种条件关系下,是否已定存在这种优美排列呢,我们用一个例子来分析,比如说当n=8,我们有数组:

1, 2, 3, 4, 5, 6, 7, 8

当我们这样有序排列的话,相邻两数的差的绝对值为1。我们想差的绝对值最大能为多少,应该是把1和8放到一起,为7。那么为了尽可能的产生不同的差的绝对值,我们在8后面需要放一个小数字,比如2,这样会产生差的绝对值6,同理,后面再跟一个大数,比如7,产生差的绝对值5,以此类推,我们得到下列数组:

1, 8, 2, 7, 3, 6, 4, 5

其差的绝对值为:7,6,5,4,3,2,1

共有7种,所以我们知道k最大为n-1,所以这样的排列一定会存在。我们的策略是,先按照这种最小最大数相邻的方法排列,没排一个,k自减1,当k减到1的时候,后面的排列方法只要按照生序的方法排列,就不会产生不同的差的绝对值,这种算法的时间复杂度是O(n),属于比较高效的那种。我们使用两个指针,初始时分别指向1和n,然后分别从i和j取数加入结果res,每取一个数字k自减1,直到k减到1的时候,开始按升序取后面的数字,参见代码如下:

解法一:

class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> res;
int i = , j = n;
while (i <= j) {
if (k > ) res.push_back(k-- % ? i++ : j--);
else res.push_back(i++);
}
return res;
}
};

下面这种方法是把上面的if...else的语句用三元操作符合并成了一句,看起来更加简洁了一些。

解法二:

class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> res;
int i = , j = n;
while (i <= j) {
res.push_back(k > ? (k-- % ? i++ : j--) : i++);
}
return res;
}
};

类似题目:

Beautiful Arrangement

参考资料:

https://discuss.leetcode.com/topic/101113/c-java-clean-code-4-liner

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Beautiful Arrangement II 优美排列之二的更多相关文章

  1. LeetCode Beautiful Arrangement II

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...

  2. LeetCode Beautiful Arrangement

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...

  3. LC 667. Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  4. [LeetCode] Beautiful Arrangement 优美排列

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  5. [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  6. 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  8. [LeetCode] Flip Game II 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  9. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

随机推荐

  1. Notepad++使用vs2015主题教程

    前言: 最近几天都在用Notepad++,所以想换个看得舒服点的主题. 发现vs2015的主题颜色特别好看.所以就查了一下有没有大佬做了这样的Notepad++主题. 结果是有的. 正文: notep ...

  2. C语言博客作业字符数组

    一.PTA实验作业 7-12 IP地址转换 本题PTA提交列表 设计思路 3.代码截图 7-7删除字符串中的子串 本题PTA提交列表 设计思路 定义字符型数组s[81]储存主串,sub[81]储存子串 ...

  3. Beta冲刺NO.1

    Beta冲刺 第一天 1. 昨天的困难 由于今天还是第一天,所以暂时没有昨天的困难. 2. 今天解决的进度 潘伟靖: 对代码进行了review 1.将某些硬编码改为软编码 2.合并了一些方法,简化代码 ...

  4. 【iOS】swift 排序Sort函数用法(包含NSDictionary排序)

    用了几分钟做的简单翻译 一个例子 直接贴代码,不过多解释 //这是我们的model class imageFile { var fileName = String() var fileID = Int ...

  5. bzoj千题计划244:bzoj3730: 震波

    http://www.lydsy.com/JudgeOnline/problem.php?id=3730 点分树内对每个节点动态维护2颗线段树 线段树以距离为下标,城市的价值为权值 对于节点x的两棵线 ...

  6. zookeeper 入门系列-理论基础 – zab 协议

    上一章讨论了paxos算法,把paxos推到一个很高的位置.但是,paxos有没有什么问题呢?实际上,paxos还是有其自身的缺点的: 1. 活锁问题.在base-paxos算法中,不存在leader ...

  7. Python之旅.第三章.函数3.28

    一.命名关键字参数: 什么是命名关键字参数?格式:在*后面参数都是命名关键字参数特点:1 必须被传值1 约束函数的调用者必须按照key=value的形式传值2 约束函数的调用者必须用我们指定的key名 ...

  8. 进军ABP第一天:ABP理论知识

    1.2.3 领域层领域层就是业务层,是一个项目的核心,所有业务规则都应该在领域层实现. ( 实体(Entity ) 实体代表业务领域的数据和操作,在实践中,通过用来映射成数据库表. ( 仓储(Repo ...

  9. php的数组的函数

    1.可以将一个二位数组转化成两个一维数组,没有指定键就是默认的索引 注意二位数组有几种类型,其中最常见的一种是外层循环是一个索引数组,然后内层是一个关联数组.这种通过便利第一层,然后第二层指定关联词就 ...

  10. SpringCloud的应用发布(二)vmvare+linux,Centos7.0下发布应用

    一.运行环境 1.jdk下载安装 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 检查是否有老版本jdk 如 ...