1. 程式人生 > >[LeetCode] Queue Reconstruction by Height 根據高度重建佇列

[LeetCode] Queue Reconstruction by Height 根據高度重建佇列

Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers(h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.

Note:
The number of people is less than 1,100.

Example

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

這道題給了我們一個佇列,佇列中的每個元素是一個pair,分別為身高和前面身高不低於當前身高的人的個數,讓我們重新排列佇列,使得每個pair的第二個引數都滿足題意。首先我們來看一種超級簡潔的方法,不得不膜拜想出這種解法的大神。首先我們給佇列先排個序,按照身高高的排前面,如果身高相同,則第二個數小的排前面。然後我們新建一個空的陣列,遍歷之前排好序的陣列,然後根據每個元素的第二個數字,將其插入到res陣列中對應的位置,參見程式碼如下:

解法一:

class Solution {
public:
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        sort(people.begin(), people.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.first > b.first || (a.first == b.first && a.second < b.second);
        });
        vector
<pair<int, int>> res; for (auto a : people) { res.insert(res.begin() + a.second, a); } return res; } };

上面那種方法是簡潔,但是用到了額外空間,我們來看一種不使用額外空間的解法,這種方法沒有沒有使用vector自帶的insert或者erase函式,而是通過一個變數cnt和k的關係來將元素向前移動到正確位置,移動到方法是通過每次跟前面的元素交換位置,使用題目中給的例子來演示過程:

[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

排序後:

[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]

交換順序:

[[7,0], [6,1], [7,1], [5,0], [5,2], [4,4]]

[[5,0], [7,0], [6,1], [7,1], [5,2], [4,4]]

[[5,0], [7,0], [5,2], [6,1], [7,1], [4,4]]

[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

解法二:

class Solution {
public:
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        sort(people.begin(), people.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
            return a.first > b.first || (a.first == b.first && a.second < b.second);
        });
        for (int i = 1; i < people.size(); ++i) {
            int cnt = 0;
            for (int j = 0; j < i; ++j) {
                if (cnt == people[i].second) {
                    pair<int, int> t = people[i];
                    for (int k = i - 1; k >= j; --k) {
                        people[k + 1] = people[k];
                    }
                    people[j] = t;
                    break;
                }
                if (people[j].first >= people[i].first) ++cnt;
            }
        }
        return people;
    }
};

下面這種解法跟解法一很相似,只不過沒有使用額外空間,而是直接把位置不對的元素從原陣列中刪除,直接加入到正確的位置上,參見程式碼如下:

解法三:

class Solution {
public:
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        sort(people.begin(), people.end(), [](const pair<int,int> &a, const pair<int, int> &b) {
            return a.first > b.first || (a.first == b.first && a.second < b.second);
        });
        for (int i = 0; i < people.size(); i++) {
            auto p = people[i];
            if (p.second != i) {
                people.erase(people.begin() + i);
                people.insert(people.begin() + p.second, p);
            }
        }
        return people;
    }
};

類似題目:

參考資料: