作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: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 2 3 4 5 6 7 8 9 10. 相邻元素的差值绝对值为1,出现了很多次。我们把后面部分的数字翻转一次,那么使得10和1临近到了一起,其他的数字的排列没有变化: 1 10 9 8 7 6 5 4 3 2,此时就有了2种不同的临近数字的差值绝对值. 继续做下去,把9到2的数字再翻转,就有了3种不同的临近数字的差值绝对值: 1 10 2 3 4 5 6 7 8 9.以此类推,找出k次即可。

class Solution(object):
def constructArray(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[int]
"""
a = list(range(1, n + 1))
for i in range(1, k):
a[i:] = a[:i-1:-1]
return a

上面的python代码是靠不停地翻转得到的,其实,也可以不用翻转,而是我们每次选取一个数字放到结果数组里面。选取的方式是从两头向中间取,这样取的时候,能够保证每次取一个数字就会产生一个不同的差值,也就需要把k减去一。当k还剩1的时候,把后面的数字从小到大排列即可,这样后面的数字的差值是1.也就是说,我们最后产生了k个不同的差值。

class Solution(object):
def constructArray(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[int]
"""
res = []
l, r = 1, n
while l <= r:
if k > 1:
if k % 2 == 1:
res.append(l)
l += 1
else:
res.append(r)
r -= 1
k -= 1
else:
res.append(l)
l += 1
return res

上面这个做法的C++代码如下:

class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> res;
int l = 1, r = n;
while (l <= r) {
if (k > 1) {
if (k % 2 == 1) {
res.push_back(l++);
} else {
res.push_back(r--);
}
k --;
} else {
res.push_back(l++);
}
}
return res;
}
};

日期

2018 年 3 月 4 日
2018 年 12 月 15 日 —— 今天四六级

【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)的更多相关文章

  1. LC 667. Beautiful Arrangement II

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

  2. 【LeetCode】90. Subsets II 解题报告(Python & C++)

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

  3. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  4. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  5. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

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

  6. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  7. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

  8. 【LeetCode】52. N-Queens II 解题报告(Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...

  9. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

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

随机推荐

  1. nginx——网站显示问题

    一般来说修改3个位置,一个是nginx.h.另一个是ngx_http_header_filter_module.c.还有一个ngx_http_special_response.c. 提示:一般修改都是 ...

  2. 4G网络 LTE、 FDD 和TD网络格式区别

    1.LTE是long term evolution的缩写,即长期演进计划,是3GPP组织推出的移动通信3G技术向4G过渡的中间标准,并不是真正意义上的4G通信. 2.FDD是移动通信系统中使用的全双工 ...

  3. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  4. 在Idea上用JDBC连接mysql数据库

    一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...

  5. 试了下GoAsm

    在VC里我们: #include <windows.h> DWORD dwNumberOfBytesWritten; int main() { HANDLE hStdOut = GetSt ...

  6. flink01--------1.flink简介 2.flink安装 3. flink提交任务的2种方式 4. 4flink的快速入门 5.source 6 常用算子(keyBy,max/min,maxBy/minBy,connect,union,split+select)

    1. flink简介 1.1 什么是flink Apache Flink是一个分布式大数据处理引擎,可以对有限数据流(如离线数据)和无限流数据及逆行有状态计算(不太懂).可以部署在各种集群环境,对各种 ...

  7. 大数据学习day26----hive01----1hive的简介 2 hive的安装(hive的两种连接方式,后台启动,标准输出,错误输出)3. 数据库的基本操作 4. 建表(内部表和外部表的创建以及应用场景,数据导入,学生、分数sql练习)5.分区表 6加载数据的方式

    1. hive的简介(具体见文档) Hive是分析处理结构化数据的工具   本质:将hive sql转化成MapReduce程序或者spark程序 Hive处理的数据一般存储在HDFS上,其分析数据底 ...

  8. 在 windows 系统上 安装与配置 PHP + Apache

    参考:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在大学时候上过一门PHP课时,因为课堂需要配置过一次PHP+Mysql ...

  9. Oracle中常用的系统函数

    本文主要来梳理下Oracle中的常用的系统函数,掌握这些函数的使用,对于我们编写SQL语句或PL/SQL代码时很有帮助,所以这也是必须掌握的知识点. 本文主要包括以下函数介绍:1.字符串函数2. 数值 ...

  10. ORACLE DBMS_ROWID包详解

    这个包在11gR2中有11个函数或存储: 1. 根据给定参数返回一个rowid --根据给定参数返回一个rowid FUNCTION rowid_create(rowid_type IN NUMBER ...