Leetcode406 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]]
題目給出一個每個人的身高和前面比自己高的人是數量,就是需要恢復出原來的序列。
我的思路比較複雜,最後樣例有幾個超時,不知道如何改進一下:
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector <pair<int, int>>& people) {
vector<pair<int, int>> result(0);
int peoplesize = people.size();
if (peoplesize == 0) {
return result;
}
pair<int, int> Pos_people = {-1,-1};
//find the shortest people with num of 0
for (int i = 0; i < people.size(); i++) {
if (people[i].second == 0 && (Pos_people.first == -1 ||
(Pos_people.first != -1 && people[i].first < Pos_people.first))
) {
Pos_people.first = people[i].first;
Pos_people.second = people[i].second;
}
}
result.push_back(Pos_people);
//vector<pair<int, int>> avaliable;
map<int, int> avaliable;
vector<pair<int, int>>::iterator it;
vector<pair<int, int>>::iterator it2;
int height, expectnum,num;
while (result.size() != peoplesize) {
//find avaliable people and add to avaliable list
for (it =people.begin(); it !=people.end(); ) {
height = (*it).first;
expectnum = (*it).second;
num = 0;
for (int j = 0; j < result.size(); j++) {
if (result[j].first >= height) {
num++;
}
}
if (expectnum == num) {
avaliable[(*it).first]=(*it).second;
it=people.erase(it);
}
else {
it++;
}
}
//find the people in vector avaliable with min height
int min, minNum;
int index=0;
min = (*avaliable.begin()).first;
minNum = (*avaliable.begin()).second;
/*
if (avaliable.size() != 0) {
min = avaliable[0].first;
minNum = avaliable[0].second;
for(int i=0;i<avaliable.size();i++)
//for (it = avaliable.begin(); it!= avaliable.end();it++)
{
if (avaliable[i].first < min) {
min = avaliable[i].first;
minNum =avaliable[i].second;
//it2 = it;
index = i;
}
}
*/
avaliable.erase(avaliable.begin());
//add to result vector
result.push_back(pair<int, int>{min, minNum});
}
return result;
}
};
最後看了一下大神程式碼,只有六行。。。
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
auto comp = [](const pair<int, int>& p1, const pair<int, int>& p2)
{ return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second); };
sort(people.begin(), people.end(), comp);
vector<pair<int, int>> res;
for (auto& p : people)
res.insert(res.begin() + p.second, p);
return res;
}
意思就是先排序,然後不斷插入而恢復序列。排序是按照身高由高到低排列,若身高相同則按人數有小到大排列。只需要一層for迴圈便可解答。首先將結點排序,h值大的結點排在前面,若兩結點的h相同,則w小的結點排在前面。舉個例子:本題的pair陣列經過上述的排序後結果如下:
【7,0】【7,1】【6,1】【5,0】【5,2】【4,4】
之後依照上面排序後的順序,依次根據w的值,選擇插入位置,例子如下:
1.將【7,0】插入空陣列
2.將【7,1】插入距離陣列首元素為1的位置,此時陣列為【7,0】【7,1】
3.將【6,1】插入距離陣列首元素為1的位置,此時陣列為【7,0】【6,1】【7,1】
4.將【5,0】插入距離陣列首元素為0的位置,此時陣列為【5,0】【7,0】【6,1】【7,1】
5.將【5,2】插入距離陣列首元素為2的位置,此時陣列為【5,0】【7,0】【5,2】【6,1】【7,1】
6.將【4,4】插入距離陣列首元素為4的位置,此時陣列為【5,0】【7,0】【5,2】【6,1】【4,4】【7,1】