[leetcode-667-Beautiful Arrangement II]
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:
- The
nandkare 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]的更多相关文章
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】667. Beautiful Arrangement II
题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...
- 667. 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 优美排列之二
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 ...
- 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- iOS之动态计算文字的高度
+ (CGSize)boundingALLRectWithSize:(NSString *)txt Font:(UIFont *)font Size:(CGSize)size { NSMutableA ...
- python 输入三个整数,按照从小到大的顺序打印
# # 3 输入三个整数,按照从小到大的顺序打印 a = int(input('请输入第一个整数:')) b = int(input('请输入第二个整数:')) c = int(input('请输入第 ...
- 国产Linux下开发正式开工(deepin)
配置开发环境 1.一般工具 在深度商店安装QQ,微信,安装一般软件WPS,Navicat数据库工具,文本编辑notepadqq. 影视娱乐爱奇艺,优酷,酷狗. 2.安装主要的开发环境 (1)c# 深度 ...
- 【js】数组添加与删除
做个表格,就会容易记忆四种方法. 返回值 是否改变数组长度 位置 功能 push() 改变数组的长度 是 末位 添加 unshift() 改变数组的长度 是 首位 添加 pop( ...
- struts2-笔记
文件下载http://struts.apache.org/download.cgi 第一步导入jar 包 在lib中有jar 包,不能把这些都导入到项目中 在apps 目录里面,找到实例程序,程序中直 ...
- UCanCode发布升级E-Form++可视化源码组件库2018全新版 !
2018年. 成都 UCanCode发布升级E-Form++可视化源码组件库2018全新版 ! --- 全面性能提升,UCanCode有史以来最强大的版本发布! E-Form++可视化源码组件库企业版 ...
- JSON字符串与JS对象格式转换
JSON通常用于服务器向客户端传送数据,传回来的JSON数据是字符串的形式,所以要转变为JS对象形式才方便我们使用. JSON字符串转变为JS对象:JSON.parse( ); JS对象转变为JSON ...
- HTML+jq简单轮播图
.main{ width: 100%; min-width: 1100px; display: table; margin: 0 auto; text-align: ce ...
- vue-cli项目使用axios实现登录拦截
登录拦截 一.路由拦截 项目中某些页面需要用户登录后才可以访问,在路由配置中添加一个字段requireAuth 在router/index.js中 . const router = new Route ...
- 集合之WeakHashMap
WeakHashMap 底层数据结构是哈希表结构 依赖于键的数据结构特点 不同于HashMap,该类的键是以弱键的形式存在 当该键成为垃圾对象,会被垃圾回收期空闲的时候回收,那么改键所对应值也会被回收 ...