【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 ...
随机推荐
- Python基础之流程控制if判断
目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...
- HTML三层界面显示
1.效果示意图 2.主要标签属性 3.实现代码 1.效果示意图 要实现类似如下效果:点击"大模态框",中间出现一层遮盖整个页面的半透明页面,最上面出现"Large mod ...
- SpringBoot整合Shiro 四:认证+授权
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...
- day10 ajax的基本使用
day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- 优化if else嵌套代码
写在前面 不知大家有没遇到过像"横放着的金字塔"一样的if else嵌套: if (true) { if (true) { if (true) { if (true) { if ( ...
- windows 查看端口被占用,解除占用
查看 (列举端口为2688) netstat -ano | findstr "2688" 解除 原文地址
- [云原生]Docker - 容器
目录 Docker容器 启动容器 新建并启动 启动已终止容器 守护态运行容器 终止容器 进入容器 attach命令 exec命令 导出和导入容器 导出容器 导入容器 删除容器 Docker容器 容器是 ...
- Mysql配置文件 16c64g优化
目录 一.说明 二.配置 一.说明 以下配置适合16核64G及以上的配置,会让性能稍微提高1/3左右. 二.配置 my.cnf [client] port = 3306 socket = /usr/l ...
- Python pyecharts绘制柱状图
本文摘抄至https://05x-docs.pyecharts.org/#/zh-cn/charts_base?id=bar%ef%bc%88%e6%9f%b1%e7%8a%b6%e5%9b%be%e ...