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】