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. 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:

    1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...

  2. VC小笔记

    1.strcpy不需要指定的长度,遇到被复制字符的串结束符’\0’才结束,容易溢出 2.memcpy(k, s, strlen(s)*sizeof(char)+1); // strlen(s) 后 + ...

  3. 757A Gotta Catch Em' All!

    A. Gotta Catch Em' All! time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. 将Win7笔记本设置成WiFi热点(无线路由器)

    查看是否能启动WiFi网卡: S1:运行cmd命令,打开命令提示符窗口(以管理员身份运行). S2:在命令行输入:netsh wlan show drivers. 若“支持的承载网络”后为“是”,则可 ...

  5. Ubuntu下笔记本触控板的禁启

    1.命令行方式,得每次用终端输入命令行设置 sudo rmmod psmouse #禁用触摸板 sudo modprobe psmouse #启用触摸板 2.永久禁用触摸板 打开终端,然后 sudo ...

  6. [Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件

    问题1:怎样通过批处理调用java代码? 问题2:怎样通过java从CSV文件获取到用户名和密码存入变量? 问题3:怎样将获取到的用户名和密码组合成字符串,写入外部批处理文件? 问题4:怎样在批处理文 ...

  7. 【JDBC&Dbutils】JDBC&JDBC连接池&DBUtils使用方法(重要)

    -----------------------JDBC---------- 0.      db.properties文件 driver=com.mysql.jdbc.Driver url=jdbc: ...

  8. 一个 图片 滚动 飞入的css特效

    @keyframes bounceInLeft { from, 60%, 75%, 90%, to {animation-timing-function: cubic-bezier(0.215, 0. ...

  9. 33. Pay Gap for the Brightest Female Graduatea 最聪明的大学女毕业生面临的工资差距

    33. Pay Gap for the Brightest Female Graduatea 最聪明的大学女毕业生面临的工资差距 ① When young women were found to ma ...

  10. SpringMVC零碎笔记

    在web.xml里可以配置webapp的默认首页,格式如下: <welcome-file-list> <welcome-file>index.html</welcome- ...