找规律

1,2,... , n 乱序排列,相邻数据的绝对差最多有n-1种

比如1,2,3,4,5对应于 1 5 2 4 3

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

python代码

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

python 代码

class Solution(object):def constructArray(self, n, k):""":type n: int:type k: int:rtype: List[int]"""res=range(1,n-k)for i in range(0,k+1):if i%2==0:#print "{} is even".format(i)res.append(n-k+i//2)else:#print "{} is odd".format(i)res.append(n-i//2)return res;

答案

Approach #2: Construction [Accepted]

Intuition

When k = n-1, a valid construction is [1, n, 2, n-1, 3, n-2, ....]. One way to see this is, we need to have a difference of n-1, which means we need 1 and n adjacent; then, we need a difference of n-2, etc.

Also, when k = 1, a valid construction is [1, 2, 3, ..., n]. So we have a construction when n-k is tiny, and when it is large. This leads to the idea that we can stitch together these two constructions: we can put [1, 2, ..., n-k-1] first so that n is effectively k+1, and then finish the construction with the first "k = n-1"method.

For example, when n = 6 and k = 3, we will construct the array as [1, 2, 3, 6, 4, 5]. This consists of two parts: a construction of [1, 2] and a construction of [1, 4, 2, 3] where every element had 2 added to it (i.e. [3, 6, 4, 5]).

Algorithm

As before, write [1, 2, ..., n-k-1] first. The remaining k+1 elements to be written are [n-k, n-k+1, ..., n], and we'll write them in alternating head and tail order.

When we are writing the ithi​th​​ element from the remaining k+1, every even ii is going to be chosen from the head, and will have value n-k + i//2. Every odd ii is going to be chosen from the tail, and will have value n - i//2.

class Solution(object):def constructArray(self, n, k):ans = list(range(1, n - k))for i in range(k+1):if i % 2 == 0:ans.append(n-k + i//2)else:ans.append(n - i//2)return ans

转载于:https://www.cnblogs.com/learning-c/p/9266391.html

667. Beautiful Arrangement II相关推荐

  1. leetcode 667. Beautiful Arrangement II | 667. 优美的排列 II(Java)

    题目 https://leetcode.com/problems/beautiful-arrangement-ii/ 题解 偏智力题,评论区有人说,A typical question testing ...

  2. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  3. LeetCode 526. Beautiful Arrangement

    526. Beautiful Arrangement Suppose you have N integers from 1 to N. We define a beautiful arrangemen ...

  4. LeetCode笔记:526. Beautiful Arrangement

    问题: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is c ...

  5. 【Leetcode】526. Beautiful Arrangement

    Description: Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array ...

  6. leetcode 526. Beautiful Arrangement | 526. 优美的排列(回溯)

    题目 https://leetcode.com/problems/beautiful-arrangement/ 题解 首先分析,全排列一个一个试的话(如下图),时间复杂度O(n^2),当n=15时,总 ...

  7. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  8. leetcode算法练习 JavaScript实现

    leetcode 表格内容由spider.js从leetcode-cn.com爬取. 已做题目答案也从leetcode-cn.com中爬取并生成文件. 解题进度:已解决 140/637 - 简单 94 ...

  9. Leetcode 部分题解

    算法思想 双指针 排序 快速选择 堆排序 桶排序 荷兰国旗问题 贪心思想 二分查找 分治 搜索 BFS DFS Backtracking 动态规划 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增 ...

最新文章

  1. R语言构建xgboost模型使用早停法训练模型(early stopping):自定义损失函数(目标函数,loss function)、评估函数(evaluation function)
  2. html语言设计表格,HTML标记语言——表格标记
  3. HDOJ1114解题报告【完全背包】
  4. JAVA——Scanner类绑定System.in后调用close()方法所引发的错误及其解决方案
  5. linux 自动保存网页,Linux 定时备份网站数据至七牛云存储
  6. linux tar cvf_Linux中的Tar命令:Tar CVF和Tar XVF通过示例命令进行了解释
  7. 目前微型计算机的内存储量一般是多大的,微型计算机内存容量的大小一般是指什么而言...
  8. 判断深度学习模型的稳定性_全自动搭建定制化深度学习模型
  9. 震撼!7月12日山东滨州,中科院向起电的雷暴云发射拖带金属导线专用火箭人工引雷!...
  10. 数据结构----排序
  11. 马斯克刚骂了激光雷达,这篇用纯视觉代替激光雷达的名校论文「力挺」了他...
  12. Kylin之Caused by :...The table :DWD_ORDER_INFO Dup key found
  13. Python 基础---列表
  14. Java Web学习视频
  15. 澳洲计算机科学专业,盘点澳洲八大名校那些最强的专业—计算机科学专业
  16. 给CSDN上原创文章添加版权声明(干货)
  17. 数据可视化,2022的春运地图
  18. 如何对华为网络产品选型
  19. 关于先有鸡还是先有蛋,终于有正确答案了
  20. 4. 假设一年期定期利率为 3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?

热门文章

  1. 前端项目难点及解决方法_预埋件施工重点难点的解决方法
  2. jquery ajax.then,jQuery动态AJAX Promise链
  3. C语言反序输出英文句子,C++实现英文句子中的单词逆序输出的方法
  4. 服务器主板北桥芯片组有哪些,主板芯片组_目前的主板芯片组都有哪些?
  5. 向量外积_解析几何 -向量
  6. yolov4Linux,基于Darknet的YOLOv4目标检测
  7. java 可以重载等于号码_Java面试之Java基础4——重载与重写的区别
  8. mysql 平均值 排序_MySQL按平均两个平均值排序
  9. flutter 国际化_Flutter 开发实战资源推荐
  10. 导出配置_Lua配置表导出优化