667. Beautiful Arrangement II
Given two integers
nandk, you need to construct a list which containsndifferent positive integers ranging from1tonand 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 exactlykdistinct 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:
- The
nandkare 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:
667. Beautiful Arrangement II的更多相关文章
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- 【leetcode】667. Beautiful Arrangement II
题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- LeetCode Beautiful Arrangement II
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...
- [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- LeetCode Beautiful Arrangement
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...
- [LeetCode] Beautiful Arrangement 优美排列
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
随机推荐
- phpcms如何给已有的模块添加新功能?
phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...
- yii的layouts的使用
yii的layouts的使用 我们在控制器中使用render()时,yii会默认的载入布局. 1.在protected/componets下的Controller.php中修改$layout变量, 来 ...
- ORA-22858: 数据类型的变更无效 varchar2类型转换为clob类型
今天遇到varchar2类型数据不够大,需改为clob类型.Oracle中,如果一个列的类型为varchar2,那么它不能直接转换为clob类型.可以通过间接的方式来修改. 就是把原来的字段删掉,重新 ...
- js 获取高度
网页可见区域宽 :document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offs ...
- PL/Sql快速执行 insert语句的.sql文件
当全是 insert语句的.sql文件太大时(insert 语句条数太大),直接打开执行sql文件,pl/sql会卡死. 这是可以用pl/sql的命令窗口来执行.sql文件,操作步骤如下: 1.新建命 ...
- win7安装qt5 纯记录 水文
去qt官网下载http://www.qt.io/看见download就点进去看看吧 目前的下载地址路径是http://www.qt.io/download-open-source选择Offline I ...
- centos_x64 6.4 安装jdk1.7
1.行到user目录下新建一个java目录 #cd /usr #mkdir java #cd /usr/java/ 2.下载jdk 先从oracle找到要下载的jdk地址然后 wget http:// ...
- DB2通用数据库性能调整的常用方法
DB2通用数据库性能调整的常用方法 DB2通用数据库性能调整的常用方法 Agenda 统计值更新--runstats 调整Buffer pool 调整日志缓冲区大小 应用程序堆大小 排序堆大小 ...
- java中配置JPA方法
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 使用JPA进行保存对象时,可以用对象来接收,例 ...
- 2018.09.16 atcoder Garbage Collector(贪心)
传送门 昨晚打比赛的时候不是很机智啊. 这道题贪心就能过了. 我们可以发现一个明显的结论,每次选的垃圾的距离从大到小排序之后,每个距离对答案的贡献的系数是5,5,7,9,11-也就是最远的是5,其余都 ...