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.

思路:

The requirement of k distinct distance can be achieved from 1, 2, ..., k+1 (<= n), by the following strategy:

1, k+1, 2, k, 3, k-1 ...;
The distance of this sequence is k, k-1, k-2, ..., 2, 1

Then append the remaining numbers to the list.

vector<int> constructArray(int n, int k) {
int l = , r = k+;
vector<int> ans;
while (l <= r) {
ans.push_back(l++);
if (l <= r) ans.push_back(r--);
}
for (int i = k+; i <= n; i++)
ans.push_back(i);
return ans;
}

参考:

https://discuss.leetcode.com/topic/101135/c-concise-code-o-n

[leetcode-667-Beautiful Arrangement II]的更多相关文章

  1. LC 667. Beautiful Arrangement II

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

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

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

  3. 【leetcode】667. Beautiful Arrangement II

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

  4. 667. Beautiful Arrangement II

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

  5. [LeetCode] Beautiful Arrangement II 优美排列之二

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

  6. LeetCode Beautiful Arrangement II

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

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

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

  8. LeetCode Beautiful Arrangement

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

  9. [LeetCode] Beautiful Arrangement 优美排列

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

  10. 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)

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

随机推荐

  1. SSH整合(一)

    一.ssh原始整合方式 不需要任何整合包,就是简单的将三个框架集合到一起 hibernate        导入jar包:            hibernate-release-5.0.7.Fin ...

  2. Spring知识点小结(二)

    一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源:        导入dbcp的jar包:dbcp+pool+connector                代码实现:  ...

  3. java8的新特性,Collections.sort(排序的List集合)的使用,对list封装Map里面的某个值进行排序

    --------------------------对简单list的排序---------------------------------- List<Integer> list = ne ...

  4. table表单制作个人简历

    应用table表单,编程个人简历表单,同时运用了跨行rowspan和跨列colspan. <!DOCTYPE html> <html> <head> <met ...

  5. python学习笔记:第18天 面向对象04-反射

    目录 issubclass和isinstance 区分函数和方法 反射 issubclass和isinstance issubclass:可以判断一个类是否另一个类的子类. # issubclass ...

  6. git小技巧之分支、关联远程仓库、回滚、解决.gitignore不生效等

    1.分支管理 新建并切换分支:git checkout -b <name>新建本地分支并关联到远程分支git checkout -b myRelease origin/Release合并某 ...

  7. bedtools-Documentation

    https://media.readthedocs.org/pdf/bedtools/latest/bedtools.pdf

  8. Java设计模式(9)——结构型模式之装饰模式(Decorator)

    一.概述 动态地给一个对象添加一些额外的职责.就增加功能来说, Decorator模式相比生成子类更为灵活.该模式以对客 户端透明的方式扩展对象的功能. UML简图 角色 在持有Component的引 ...

  9. Android开发——Context类的各种细节问题

    0. 前言   Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学习一下那些你所不知道的细节. ...

  10. 【NAS】CIFS用户场景需求分析

    1.everyone用户 1.1: 场景描述:共享目录为rule,所有用户都可以查看,但是不能修改: 解决方法:在smb.conf里配置read only = yes,具体示例如下: [rule] p ...