【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛
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:
- 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++)的更多相关文章
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【LeetCode】275. H-Index II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...
- 【LeetCode】52. N-Queens II 解题报告(Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 全排列函数 回溯法 日期 题目地址:https:// ...
- 【LeetCode】454. 4Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
随机推荐
- KVM原理
虚拟化是云计算的基础.简单的说,虚拟化使得在一台物理的服务器上可以跑多台虚拟机,虚拟机共享物理机的 CPU.内存.IO 硬件资源,但逻辑上虚拟机之间是相互隔离的.物理机我们一般称为宿主机(Host), ...
- Python中类的各式方法介绍
本文类的方法介绍包括类方法.属性方法.静态方法.修改属性方法等内置装饰器装饰的方法,以及类的一些特殊成员方法 1. 类的特殊成员方法 1.1 构造方法 # -*- coding:utf-8 -*- # ...
- 使用flock命令查看nas存储是否支持文件锁
上锁 文件锁有两种 shared lock 共享锁 exclusive lock 排他锁 当文件被上了共享锁之后,其他进程可以继续为此文件加共享锁,但此文件不能被加排他锁,此文件会有一个共享锁计数,加 ...
- 『学了就忘』Linux文件系统管理 — 64、磁盘配额的配置步骤
目录 1.手工建立一个5GB的分区 2.建立需要做限制的三个用户 3.在分区上开启磁盘配额功能 4.建立磁盘配额的配置文件 5.开始设置用户和组的配额限制 6.启动和关闭配额 7.磁盘配额的查询 8. ...
- 在C++的map类型中按value排序
1.将map转化为vector类型 2.使用sort函数对vector进行排序,写出compare比较器函数 3.比较器中指明按照第几个元素来排序 1 #include <iostream> ...
- 巩固javaweb的第二十六天
正则表达式 正则表达式提供了一种高级的.但不直观的字符串匹配和处理的方法.它描述了一种 字符串匹配的模式,可以用来判断一个字符串是否满足某种格式,或者一个字符串是否含 有某个子串等. 1. 字符集 正 ...
- linux 实用指令时间日期类
linux 使用指令时间日期类 data 显示当前日期 基本语法 date 显示当前时间 date+%Y 显示当前年份 date+%m 显示当前月份 date+%d 显示当前是哪一天 date &qu ...
- SELECT的语法
我们先回顾下正则表达式.下图: 描述像xy, xxy (B上转一圈), xyy, xxyy这样的字符串.然后可以进行字符串匹配.设计芯片都用Verilog语言而不是画门电路了.像x+y+这样的叫做re ...
- 大数据学习day24-------spark07-----1. sortBy是Transformation算子,为什么会触发Action 2. SparkSQL 3. DataFrame的创建 4. DSL风格API语法 5 两种风格(SQL、DSL)计算workcount案例
1. sortBy是Transformation算子,为什么会触发Action sortBy需要对数据进行全局排序,其需要用到RangePartitioner,而在创建RangePartitioner ...
- 【leetcode】43. Multiply Strings(大数相乘)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...