原题链接在这里: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. offsetHeight+scrollHeight+clientHeight

    ch 窗口可见区域高度 :ch = padding + height(height不是所有内容的高度,是样式定义的高度) oh border以内的内容高度: oh = border + padding ...

  2. 乐思启慧教学系列—Bootstrap布局规则

    1外层变化,内层相应变化规则 col-md-6 col-md-4 外层6变成12,扩大了2倍,里面就得缩小2倍(除以2), 只有这样才能保持外部变化了,内部依然对齐 col-md-12 col-md- ...

  3. Socket 例子

    package com.pab.util; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import ...

  4. 定制AIX操作系统的shell环境

    操作系统与外部最主要的接口就叫做shell.shell是操作系统最外面的一层.shell管理你与操作系统之间的交互:等待你输入,向操作系统解释你的输入,并且处理各种各样的操作系统的输出结果. shel ...

  5. 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle

    BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...

  6. 一个gpio 不受控制的bug

    前几天调试一个flash灯的驱动程序,这可ic 有两个控制pin, 一个叫en1 一个叫en2, 根据spec的说明,不同的组合将产生不同的输出电流.但我发现,那个en1 这个pin 死活是拉不高的, ...

  7. JMeter学习(十三)JMeter使用中遇到的问题:Jmeter Debug - "Unrecognized VM option '+HeapDumpOnOutOfMemoryError"

    启动JMeter.bat的程序时,出现以下出错信息: Unrecognized VM option '+HeapDumpOnOutOfMemoryError' Could not create the ...

  8. jquery02-jQuery效果=隐藏和显示+切换+淡入淡出+滑动+动画+回调+链

    隐藏和显示 $(selector).hide(speed,callback);  $(selector).show(speed,callback);   可选的 speed 参数规定隐藏/显示的速度, ...

  9. Java中初始变量默认值

    Java语言中有8种基本数据类型,基本情况汇总如下: 序号 数据类型 大小/位 封装类 默认值 可表示数据范围 1 byte(位) 8 Byte 0 -128~127 2 short(短整数) 16 ...

  10. App测试经验分享之登录注册

    要诀 另外自己总结了一些要诀,仅供参考: 1)快:快速操作,营造冲突的场景,例如加载过程中返回键交互,快速点击登录按钮,快速切换菜单项,快速多次上下拉刷新 2)变:手机横竖屏.手机切换语言.手机调整字 ...