LC 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 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 = 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.
Runtime: 28 ms, faster than 39.80% of C++ online submissions for Beautiful Arrangement II.
构造题
最小,k=1是顺序或者倒序的排列。
最大,k=n-1是依此从两头挑数的排列。
根据k来判断要排多少个头尾数。
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> constructArray(int n, int k) {
int tmp = k/;
vector<int> ret;
int idx = ;
while(tmp){
ret.push_back(idx);
ret.push_back(n+ - idx);
idx++;
tmp--;
}
if(k % == ){
for(int i = idx; i<= n+-idx; i++) ret.push_back(i);
}else for(int i=n+-idx; i>=idx; i--) ret.push_back(i);
return ret;
}
};
LC 667. Beautiful Arrangement II的更多相关文章
- 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
题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- LC 526. Beautiful Arrangement
uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...
- [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 ...
随机推荐
- Linux配置JDK环境
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- PAT Basic 1021 个位数统计 (15 分)
给定一个 k 位整数 1 (0, ,, dk−1>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...
- SQL语句 case ... when
select 字段A, 字段B, CASE WHEN t.operate = 1 then 'aa'WHEN t.operate = 2 then 'bb'WHEN t.operate = 3 the ...
- java 发布订阅
https://www.cnblogs.com/coderdxj/p/9627310.html java 观察这模式(发布订阅模式) 观察者设计模式定义了对象间的一种一对多的组合关系,以便一个对象 ...
- request_time和upstream_response_time详解
下图是request_time. 下图是upstream_response_time. 精准的描述就是:request_time是从接收到客户端的第一个字节开始,到把所有的响应数据都发送完为止.ups ...
- 小程序生成海报 canvas
前言 微信小程序需要生成海报进行朋友圈分享,但是不同的手机会有问题, 然后首先是图片的问题 图片 在模拟器上没有报错,可是真机测试却什么也没画出来. canvas.drawImage 是不支持网络图片 ...
- TXNLP 09-17
上节课讲了一些算法的复杂度,都比较简单,我就没有单独截图.1 n n^2 nlogn logn...等等 其实一些排序问题也比较简单.还是给大家列举一下. 归并排序: 主定理定理..吐血 算法复杂度相 ...
- Acwing-169-数独2(搜索, 剪枝)
链接: https://www.acwing.com/problem/content/171/ 题意: 请你将一个16x16的数独填写完整,使得每行.每列.每个4x4十六宫格内字母A~P均恰好出现一次 ...
- [Functional Programming] Examples: When and Unless
/** * When */ const _branch = (x) => { const result = (x && x.isPublic) ? dissoc('private ...
- Spring中 bean的生命周期
为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...