[LeetCode] 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.

 

Note:

  1. The n and k are in the range 1 <= k < n <= 104.

 

這道題雖然也叫優美排列,但是貌似跟之前那道Beautiful Arrangement的關系不太大。這道題給我們了一個數字n和一個數字k,讓找出一種排列方式,使得1到n組成的數組中相鄰兩個數的差的絕對值正好有k種。給了k和n的關系為k<n。那么我們首先來考慮,是否這種條件關系下,是否已定存在這種優美排列呢,我們用一個例子來分析,比如說當n=8,我們有數組:

1, 2, 3, 4, 5, 6, 7, 8

當我們這樣有序排列的話,相鄰兩數的差的絕對值為1。我們想差的絕對值最大能為多少,應該是把1和8放到一起,為7。那么為了盡可能的產生不同的差的絕對值,我們在8后面需要放一個小數字,比如2,這樣會產生差的絕對值6,同理,后面再跟一個大數,比如7,產生差的絕對值5,以此類推,我們得到下列數組:

1, 8, 2, 7, 3, 6, 4, 5

其差的絕對值為:7,6,5,4,3,2,1

共有7種,所以我們知道k最大為n-1,所以這樣的排列一定會存在。我們的策略是,先按照這種最小最大數相鄰的方法排列,沒排一個,k自減1,當k減到1的時候,后面的排列方法只要按照生序的方法排列,就不會產生不同的差的絕對值,這種算法的時間復雜度是O(n),屬於比較高效的那種。我們使用兩個指針,初始時分別指向1和n,然后分別從i和j取數加入結果res,每取一個數字k自減1,直到k減到1的時候,開始按升序取后面的數字,參見代碼如下:

 

解法一:

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

 

下面這種方法是把上面的if...else的語句用三元操作符合並成了一句,看起來更加簡潔了一些。 

 

解法二:

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

 

類似題目:

Beautiful Arrangement

 

參考資料:

https://discuss.leetcode.com/topic/101113/c-java-clean-code-4-liner

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM