leetcode- Beautiful Arrangement II
来源:互联网 发布:阿里云 短信 编辑:程序博客网 时间:2024/06/05 08:20
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 n and k are in the range
思路:假设每一次 k = n-1
通过以上可以看出当 k=n-1 时,满足条件的序列都满足 min - max - min+1 - max-1 - min+1+1 - max-1-1 …
因此对于给出的任意
class Solution { public int[] constructArray(int n, int k) { int tail = k+1; int front = 1; int[] ans = new int[n]; for(int i = 0; i <= k; i++){ if(i%2 == 0){ ans[i] = front++; }else{ ans[i] = tail--; } } for(int i = k+2; i <= n; i++){ ans[i-1] = i; } return ans; }}
- leetcode- Beautiful Arrangement II
- leetCode-Beautiful Arrangement II
- LeetCode 667. Beautiful Arrangement II
- leetcode 667. Beautiful Arrangement II
- LeetCode 667. Beautiful Arrangement II
- LeetCode 667. Beautiful Arrangement II
- leetcode 667. Beautiful Arrangement II
- LeetCode 667. Beautiful Arrangement II
- leetcode 667. Beautiful Arrangement II
- leetcode Beautiful Arrangement II 优美排列
- 667. Beautiful Arrangement II
- 667. Beautiful Arrangement II
- 667. Beautiful Arrangement II
- 667. Beautiful Arrangement II
- 667. Beautiful Arrangement II。
- 667. Beautiful Arrangement II
- Leetcode Beautiful Arrangement
- LeetCode 526. Beautiful Arrangement
- Scala入门笔记
- NC开发笔记——单据模板编辑显示公式使用
- C#中Socket通信编程的异步实现
- iOS 判断网络连接状态的几种方法
- Foursquare数据集处理之利用defaultdict进行主次关键字排序
- leetcode- Beautiful Arrangement II
- RecyclerView 多布局使用
- httpURLConnection
- 5-1 使用函数计算分段函数的值
- Android中的ContentProvider小结
- Unity Mono.Data.Sqlite.dll System.Data.dll路径问题
- CRC16
- 解决清除浮动的问题
- kettle学习导航帖整理