原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/

题目:

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.

题解:

头k-1个数按照1, n, 2, n-1, 3, n-2...的规律, diff的绝对值是n-1, n-2, n-3减小. 剩下的部分都按照diff是1增长.

Time Complexity: O(n).

Space: O(n), res space.

AC Java:

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

类似Beautiful Arrangement.

LeetCode Beautiful Arrangement II的更多相关文章

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

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

  2. LeetCode Beautiful Arrangement

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

  3. LC 667. Beautiful Arrangement II

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

  4. [LeetCode] Beautiful Arrangement 优美排列

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

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

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

  6. 【leetcode】667. Beautiful Arrangement II

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

  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. 667. Beautiful Arrangement II

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

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

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

随机推荐

  1. 设计模式之单例模式--instance

    <?php header('Content-Type:text/html;charest=utf-8'); /** * 设计模式之单例模式 * $_instance必须声明为静态的私有变量 * ...

  2. jQuery中的部分方法

    1.empty() – jQuery 文档操作 从被选元素移除所有内容,包括所有文本和子节点. 用法:$(selector).empty(); 其中,selector可以是"#id" ...

  3. GPU的工作原理

    转:https://blog.csdn.net/p23onzq/article/details/79609629 在GPU出现以前,显卡和CPU的关系有点像“主仆”,简单地说这时的显卡就是画笔,根据各 ...

  4. PHP自动导入类

    自动require出所需要的类文件,支持PSR系列规范 spl_autoload_register(function ($class) { $classNames = explode('\\',$cl ...

  5. 13.常见模块re-正则模块

    1.正则 正则表达式是计算机科学的一个概念,正则表通常被用来检索.替换那些符合某个模式(规则)的文本.也就是说使用正则表达式可以在字符串中匹配出你需要的字符或者字符串,甚至可以替换你不需要的字符或者字 ...

  6. pache—DBUtils框架简介、DbUtils类、QueryRunner类 、ResultSetHandler接口

    Apache—DBUtils框架简介.DbUtils类.QueryRunner类 .ResultSetHandler接口 commons-dbutils 是 Apache 组织提供的一个开源 JDBC ...

  7. 让iOS项目允许使用http协议请求

    苹果官方已经默认不让开发者使用不安全的http通信协议了,而是建议开发者使用安全的https协议.若我们还是需要使用http协议可以这样配置XCode: 1.打开info.plist文件 2.点击加号 ...

  8. How to choose from Viewstate, SessionState, Cookies and Cache

    https://devshop.wordpress.com/2008/04/10/how-to-choose-from-viewstate-sessionstate-cookies-and-cache ...

  9. nginx 反向代理配置之---可配置多域名请求

    配置文件如下: server { listen 80; server_name ngin服务器所对应的的域名; error_log /data/logs/nginx/mainsite.error.lo ...

  10. django学习笔记整理(2)django的路由系统

    创建一个属于你的django框架.django-admin startproject 框架名 打开你的django框架,你会发现里面还有一个和框架名同名的文件夹,那个文件夹就称为一个app. 创建了d ...