您的位置:首页 > 编程语言

编程练习(第八周)

2017-04-15 10:37 176 查看


406. Queue Reconstruction by Height

Add to List

DescriptionHintsSubmissionsSolutions

Total Accepted: 20635
Total Submissions: 37676
Difficulty: Medium
Contributor: LeetCode

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.
题解:
bool comp(const pair<int, int>& p1, const pair<int, int>& p2){
return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);
}

vector< pair<int, int> > reconstructQueue(vector< pair<int, int> >& people) {
sort(people.begin(), people.end(), comp);
vector< pair<int, int> > res;
for (int i=0; i<people.size(); i++)
res.insert(res.begin() + people[i].second, people[i]);
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: