再从萌新开始-Leetcode每日题解-667. Beautiful Arrangement II
2018-01-10 10:16
399 查看
667. Beautiful
Arrangement II(Medium)
Given two integers
you need to construct a list which contains
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
If there are multiple answers, print any of them.
Example 1:
Example 2:
Note:
The
in the range 1 <= k < n <= 104.
------------------------------------------------分割线------------------------------------------------------------
题目给定n和k,要求构造一个数组,使得数值包括[1,n]中的所有,并且前后两个的差值的绝对值([|a1 -
a2|,
|a2 -
a3|,
|a3 -
a4|,
... , |an-1 -
an|]) 只有k个不同值。数据量大小为1
<= k < n <= 104,意思就是时间的复杂度要求是O(n^2)以下,讲道理应该是一遍扫描O(n)就可以完成。
分析:
只要找好切入点还是很容易解决的。考虑[1,n]的数组的差值最大可以有几个,该怎么排列:
1. 最大的差值肯定是n-1 = | n - 1 |, 排列可以是 (n,1) 或者是(1,n)。选择其中一种(比如说(1,n)继续往下考虑);
2.那差值 n-2 = | n - 2 | or | (n -1) - 1 |,如果考虑到前面的排列,选择(n , 2)试试,排列就变成了(1, n, 2);
3.同理,顺着这种构造方法下去,我们可以获得拥有m个元素的,并且有m-1个不同差值的排序(1,n,2,n-1,3,…),当m为偶数时,最后一项是n+1-m/2; 当m为奇数时,最后一项是m/2+1;
4.最后一步,就是如何控制差值恰好为k个。以上,我们已经可以通过构造得到k = n-1的情况(也是最大的k值)。我用的方法是,留下最后一个k的位置给1,举个例子来说明吧:如果结构得到了(1,
n, 2, n-1 ,3),其中有4个不同差值,如果k等于5,恰好还剩下一个“位置”,那最后一步就是按照升序将剩下的值填充进去,即变成(1, n, 2, n-1, 3,
4, 5, 6 ,7
…, n-1, n-2),让剩下的所有项的差值都为1。
代码:
class Solution{
public:
vector<int> constructArray(int n,int k){
vector<int> ans;
int left = 1, right = n;
bool flag = 1; // 1 for left and 0 for right
while (k > 1){
if (flag){
ans.push_back(left);
left++;
}
else{
ans.push_back(right);
right--;
}
flag = !flag;
k--;
}
while (left <= right){
if (flag){
ans.push_back(left);
left++;
}else{
ans.push_back(right);
right--;
}
}
return ans;
}
}; 其中两个while其实可以合并成一个,可以代码会更加简短,虽然降低了可读性。
-------------------------------------------分割线-----------------------------------------------------
题解后面再补。
Arrangement II(Medium)
Given two integers
nand
k,
you need to construct a list which contains
ndifferent positive integers ranging from
1to
nand
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
kdistinct 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 = 2Output:[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
nand
kare
in the range 1 <= k < n <= 104.
------------------------------------------------分割线------------------------------------------------------------
题目给定n和k,要求构造一个数组,使得数值包括[1,n]中的所有,并且前后两个的差值的绝对值([|a1 -
a2|,
|a2 -
a3|,
|a3 -
a4|,
... , |an-1 -
an|]) 只有k个不同值。数据量大小为1
<= k < n <= 104,意思就是时间的复杂度要求是O(n^2)以下,讲道理应该是一遍扫描O(n)就可以完成。
分析:
只要找好切入点还是很容易解决的。考虑[1,n]的数组的差值最大可以有几个,该怎么排列:
1. 最大的差值肯定是n-1 = | n - 1 |, 排列可以是 (n,1) 或者是(1,n)。选择其中一种(比如说(1,n)继续往下考虑);
2.那差值 n-2 = | n - 2 | or | (n -1) - 1 |,如果考虑到前面的排列,选择(n , 2)试试,排列就变成了(1, n, 2);
3.同理,顺着这种构造方法下去,我们可以获得拥有m个元素的,并且有m-1个不同差值的排序(1,n,2,n-1,3,…),当m为偶数时,最后一项是n+1-m/2; 当m为奇数时,最后一项是m/2+1;
4.最后一步,就是如何控制差值恰好为k个。以上,我们已经可以通过构造得到k = n-1的情况(也是最大的k值)。我用的方法是,留下最后一个k的位置给1,举个例子来说明吧:如果结构得到了(1,
n, 2, n-1 ,3),其中有4个不同差值,如果k等于5,恰好还剩下一个“位置”,那最后一步就是按照升序将剩下的值填充进去,即变成(1, n, 2, n-1, 3,
4, 5, 6 ,7
…, n-1, n-2),让剩下的所有项的差值都为1。
代码:
class Solution{
public:
vector<int> constructArray(int n,int k){
vector<int> ans;
int left = 1, right = n;
bool flag = 1; // 1 for left and 0 for right
while (k > 1){
if (flag){
ans.push_back(left);
left++;
}
else{
ans.push_back(right);
right--;
}
flag = !flag;
k--;
}
while (left <= right){
if (flag){
ans.push_back(left);
left++;
}else{
ans.push_back(right);
right--;
}
}
return ans;
}
}; 其中两个while其实可以合并成一个,可以代码会更加简短,虽然降低了可读性。
-------------------------------------------分割线-----------------------------------------------------
题解后面再补。
相关文章推荐
- 再从萌新开始-Leetcode每日题解-315. Count of Smaller Numbers After Self
- 再从萌新开始-Leetcode每日题解-345. Reverse Vowels of a String
- 再从萌新开始-Leetcode每日题解-160. Intersection of Two Linked Lists
- 再从萌新开始-Leetcode每日题解-153. Find Minimum in Rotated Sorted Array
- 再从萌新开始-Leetcode每日题解-287. Find the Duplicate Number
- 再从萌新开始-Leetcode每日题解-445. Add Two Numbers II
- 再从萌新开始-Leetcode每日题解-447. Number of Boomerangs
- 再从萌新开始-Leetcode每日题解-740. Delete and Earn
- [LeetCode]题解(python):133-Clone Graph
- [LeetCode]题解(python):142-Linked List Cycle II
- [LeetCode]题解(python):135-Candy
- leetcode题解-217. Contains Duplicate&&219.Contains Duplicate II
- leetcode题解-28. Implement strStr() && 521. Longest Uncommon Subsequence I && 522 II
- leetcode326题 题解 翻译 C语言版 Python版
- LeetCode 343. Integer Break 题解(C++)
- Leetcode题解 190. Reverse Bits
- leetcode题解日练--2016.9.01
- [LeetCode 题解]: Insertion Sort List
- leetcode题解-36.Valid Sudoku
- 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解