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.

Approach #1: Math. [Java]

class Solution {
public int[] constructArray(int n, int k) {
int[] res = new int[n]; for (int i = 0, l = 1, r = n; l <= r; ++i) {
res[i] = (k > 1) ? (k-- % 2 != 0 ? l++ : r--) : l++;
} return res;
}
}

  

Analysis:

https://leetcode.com/problems/beautiful-arrangement-ii/discuss/106948/C%2B%2B-Java-Clean-Code-4-liner

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

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

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

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

  4. [LeetCode] 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

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

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

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

  7. LeetCode Beautiful Arrangement

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

  8. [LeetCode] Beautiful Arrangement 优美排列

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

  9. [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement

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

随机推荐

  1. vsftp 500 OOPS: cannot change directory:/home/xyp

    1.在linux终端下输入: > setsebool ftpd_disable_trans 1 (*如果出现Could not change active booleans: Invalid b ...

  2. htons、htonl与字节序大小端

    判断字节序大小端code #include <stdio.h> int main() { ) == ) printf("big endian\n"); else pri ...

  3. 关于MySQL在内网中使用另一台机器访问的问题

    要在内网中访问另一台机器的MySQL数据库,需要两步操作 一是把运行MySQL的机器的3306端口打开,最好是能限制访问IP保证安全性. 二是更改MySQL账户的访问权限.MySQL的root账户默认 ...

  4. $.post 提示错误: Uncaught SyntaxError: Unexpected token :

    $.post("addRecommond",{"productId":productId,"categoryCode":categoryCo ...

  5. 复制文件描述符---dup

    函数功能:复制文件描述符 头文件:#include<unistd.h> 函数原型:int dup(int oldfd) 参数说明:oldfd:旧的文件描述符 返回值:成功返回-个新的文件描 ...

  6. Linux下JDK应该安装在哪个位置

    在百度知道上看到的回答觉得不错:https://zhidao.baidu.com/question/1692690545668784588.html 如果你认为jdk是系统提供给你可选的程序,放在op ...

  7. Spring+SpringMVC+mybatis+Quartz整合

    Quartz与SpringMVC的整合 简介 Quartz是一个完全由java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制.Quartz允许开发人员根据时间间隔来调 ...

  8. linux的零碎知识

    一  nfs服务器 1  NFS的介绍:是Network File System的简写,是网络文件系统.用于分散式文件系统的协定,由sun公司开发的,在1984年向外公布的. 2  NFS的功能:是通 ...

  9. 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)

    传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...

  10. python pip国内源

    pip国内的这个源最快   清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 修改源方法: 临时使用: 可以在使用pip的时候在后面加上-i参数,指定pip ...