Leetcode667. 求一种数组排列方式使数组相邻数值对有k个不同绝对值

来源:互联网 发布:淘宝比较好的吉他店 编辑:程序博客网 时间:2024/06/14 17:18

Leetcode667. Beautiful Arrangement II

题目

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.

解题分析

看到这道题可能开始又没有什么思路,规律并不是那么好找。我们可以这样想,如果是一种很普通的排序,那么想要找到一种算法满足这个要求可能会很难,因此这道题肯定有某一种排序方法是有迹可循的。
要想让数组n个元素相邻的数值对有k个不同的绝对值,由于有n-1个间隔,所以总共有(n-1)-(k-1)=(n-k)个间隔的绝对值是相同的。而很显然,这(n-k)个绝对值相同的间隔的绝对值必为1,因为要考虑到k=1的情况。因此,当确定了这(n-k)个间隔绝对值为1后,由于我们不确定k为何值,我们可以考虑将其它(k-1)个间隔的绝对值从n-1按从大到小排列。
确定了这个之后,问题就简单很多了,只需确定首个元素,这里推荐选用数组首末两个元素中的一个,如果选用中间元素,还需要取余转换,相对来说比较麻烦。最后只需用一个变量来确定绝对值里面的元素何时为正何时为负就可以了,问题就顺利解决了,这种方法是不是很巧妙呢?

源代码

class Solution {public:    vector<int> constructArray(int n, int k) {        vector<int> v;        int i, count = 0, number, num;        v.push_back(n);        for (i = k - 1; i > 0; i--) {            number = v.back();            count++;            num = n - count;            if (count % 2 == 1) {                v.push_back(number - num);            }            else {                v.push_back(number + num);            }        }        for (i = n - k - 1; i >= 0; i--) {            number = v.back();            if (count % 2 == 1) {                v.push_back(number + 1);            }            else {                v.push_back(number - 1);            }        }        return v;    }};

以上是我对这道问题的一些想法,有问题还请在评论区讨论留言~

阅读全文
0 0
原创粉丝点击