classSolution { public: vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { sort(people.begin(), people.end(), [](constauto & a, constauto & b) { int A = a.first, B = b.first; // [[A, aa], [B, bb]] int aa = a.second, bb = b.second; if (A == B) { return aa <= bb; }else { return A > B; } }); for (int i = 0; i < people.size(); i++) { pair<int, int> num = people[i]; int index = 0; int count = num.second; while(index < i && count > 0) { // 找到应该在哪些大于它的元素后面的位置 if (num.first <= people[index].first) { count--; } index++; if (count == 0) break; } for (int k = i; k > index; k--) { // 找到后,将它之后的元素向后挪一个位置出来 people[k] = people[k-1]; } people[index] = num; } return people; } };