【leetcode】667. Beautiful Arrangement II
题目如下:
Given two integers
n
andk
, you need to construct a list which containsn
different positive integers ranging from1
ton
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 exactlyk
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
andk
are in the range 1 <= k < n <= 104.
解题思路:感觉自己对“给定一个条件,输出对应排列”这一类型的题目比较没有思路。本题的解法我也是想了很久才想出来。以[1,2,3,4,5,6]为例,当前k是等于1的;假设要k=2,只需要把6插入到头部[6,1,2,3,4,5]即可。而如果要k=3,那么把6插入到1的后面,变成[1,6,2,3,4,5]。接下来就是递推了,记插入6的位置为inx,要使得k=4,只要在k=2的基础上把最后一个元素插入到inx+2的位置;同理,如果是k=5,就在k=3的基础上操作。
代码如下:
class Solution(object):
def constructArray(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[int]
"""
res = [i for i in range(1,n+1)]
inx = 0
if k % 2 == 1:
inx = 1
else:
res.insert(0, res.pop(-1))
k -= 1
inx += 2
while k > 1:
res.insert(inx,res.pop(-1))
inx += 2
k -= 2
return res
【leetcode】667. Beautiful Arrangement II的更多相关文章
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
随机推荐
- JAVA读取Excel2003、2007、2010教程
import java.io.File;import java.io.FileInputStream;import org.apache.poi.ss.usermodel.Row;import org ...
- find及其他命令
Find命令 Find / -type f :f为普通文件 Find / -name *.txt :查找.txt结尾的 Find / -size +30M :找根目录下大于30M的文件 F ...
- linux基础(六)
今天我们来看一下Samba服务和nginx服务. Samba服务 1.samba的功能 samba是一个网络服务器,用于Linux和Windows之间共享文件. 2.samba服务的启动.停止.重启 ...
- 低价购买 (动态规划,变种最长下降子序列(LIS))
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- win7 SP1 原版 32位 百度网盘下载
下载地址:https://pan.baidu.com/s/1o6I410XduG1kcmn9vQ3miw 提取码:15vm 扫码下载:
- Angular 2 技能图谱skill-map
# Angular 2 技能图谱 ## 模块 ### 自定义模块 - 根模块 - 特性模块 - 共享模块 - 核心模块 ### 内置模块 - ApplicationModule 模块 - Common ...
- Webx.0-Web4.0:Web4.0
ylbtech-Webx.0-Web4.0:Web4.0 Web系统是人类迄今最伟大的发明之一,也是计算机影响人类最深远的表现. 1.返回顶部 1. Web系统是人类迄今最伟大的发明之一,也是计算机影 ...
- 百度上有个最难数独, 用python跑它
直接上代码 #!/usr/bin/python3 #coding=GB2312 import tkinter as tk import threading import time import ran ...
- spss-数据清洗-处理重复数据
spss-数据清洗-处理重复数据 数据导入之后就需要对数据进行清洗.数据清洗主要是对多余重复的数据筛选清除,将缺失的数据补充完整,将错误的数据纠正或者删除.接下来操作如何将重复数据处理操作. 步骤一: ...
- 将日志(Microsoft.Extensions.Logging)添加到.NET Core控制台应用程序
在.NET Core项目中,日志记录是通过依赖项注入进行管理的. 尽管这对于ASP.NET项目效果很好,但在启动Startup.cs中的新项目时,所有这些都会自动创建,而在控制台应用程序中则需要一些配 ...