假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。
请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。
示例 1:
输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
解释:
编号为 0 的人身高为 5 ,没有身高更高或者相同的人排在他前面。
编号为 1 的人身高为 7 ,没有身高更高或者相同的人排在他前面。
编号为 2 的人身高为 5 ,有 2 个身高更高或者相同的人排在他前面,即编号为 0 和 1 的人。
编号为 3 的人身高为 6 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
编号为 4 的人身高为 4 ,有 4 个身高更高或者相同的人排在他前面,即编号为 0、1、2、3 的人。
编号为 5 的人身高为 7 ,有 1 个身高更高或者相同的人排在他前面,即编号为 1 的人。
因此 [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] 是重新构造后的队列。
示例 2:
输入:people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
输出:[[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]
提示:
1 <= people.length <= 2000
0 <= hi <= 106
0 <= ki < people.length
题目数据确保队列可以被重建
这个题,差点把我写道猝死。
具体思路就是
先按照元素1,即arr[i][0]进行降序,
然后按照元素2,arr[i][1]进行升序。
然后依次与result数组的大小做比较。
(1)遍历的数(第二个元素):大于等于构造数组的长度,放到最后,
因为当前插入的身高一定比构造数组中的所有身高矮。
(2)遍历的数(第二个元素):小于构造数组的长度,放到指定位置上
(即元素2是多少,放到哪里)
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> result;
/* 第一个数按照降序 */
firstKeyQSort(people, 0, people.size() - 1);
//printTest(people);
/* 第二个数按照升序,在身高已经有序的情况 */
SecondSort(people);
//printTest(people);
for (int i = 0; i < people.size(); i++) {
/*
* 如果后面的数比数组的大于等于构造数组的长度,
* 则放到最后,因为当前插入的是身高最高的人
*/
if (result.size() <= people[i][1]) {
result.push_back(people[i]);
}
/*
* 否则到指定位置插入,因为是降序的
* 即使ki是相等的(即前面有ki个身高大于等于hi的人)
* 也是按照指定位置插入,身高最高的先插入,会被顶到后面
* 符合题意
*/
else {
result.insert(result.begin() + people[i][1], people[i]);
}
}
return result;
}
void printTest(vector<vector<int>>& people) {
for (int i = 0; i < people.size(); i++) {
for (int j = 0; j < people[i].size(); j++) {
cout << people[i][j] << ",";
}
cout << endl;
}
cout << endl;
cout << "print over!" << endl;
cout << endl;
}
public:
/* 降序,根据元素1来进行排序 */
void firstKeyQSort(vector<vector<int>>& arr, int left, int right) {
if (left >= right) {
return;
}
int i = left;
int j = right;
int key1 = arr[i][0];
while (i < j) {
while (i < j && arr[j][0] <= key1) {
j--;
}
swap(arr[i], arr[j]);
while (i < j && arr[i][0] >= key1) {
i++;
}
swap(arr[i], arr[j]);
}
arr[i][0] = key1;
firstKeyQSort(arr, left, i - 1);
firstKeyQSort(arr, i + 1, right);
}
/* 升序,元素1相等的情况根据元素2来升序 */
void SecondSort(vector<vector<int>>& arr) {
int index1 = 0;
int index2 = 0;
vector<int>i1;
vector<int>i2;
for (int i = 0; i < arr.size() - 1; i++) {
if (arr[i][0] != arr[i + 1][0]) {
index2 = i;
i1.push_back(index1);
i2.push_back(index2);
index1 = index2 + 1;
}
}
/* 最后一位 */
if (index2 < arr.size() - 1) {
i1.push_back(index2 + 1);
i2.push_back(arr.size() - 1);
}
for (int i = 0; i < i1.size(); i++) {
int start = i1[i];
int end = i2[i];
//cout << "start:" << start << "--------end:" << end << endl;
for (int j = start; j < end; j++) {
for (int k = start; k < end; k++) {
if (arr[k][1] > arr[k + 1][1]) {
swap(arr[k], arr[k + 1]);
}
}
}
}
}
};
//手动演算
//[9,0]
//[7,0]
//[6,0]
//[6,2]
//[5,2]
//[5,3]
//[3,0]
//[3,4]
//[2,7]
//[1,9]
//[3,0],[6,0],[7,0],[5,2],[3,4],[5,3],[6,2],[2,7],[9,0], [1, 9]
int main() {
//vector<vector<int>>people = { {7, 0},{4, 4},{7, 1},{5, 0},{6, 1},{5, 2} };
//vector<vector<int>>people = { {9, 0}, {7, 0}, {1, 9}, {3, 0}, {2, 7}, {5, 3}, {6, 0}, {3, 4}, {6, 2}, {5, 2} };
vector<vector<int>>people = { {8, 2},{4, 2 },{4, 5},{2, 0},{7, 2},{1, 4},{9, 1},{3, 1},{9, 0},{1, 0} };
vector<vector<int>>result;
Solution* ps = new Solution();
result = ps->reconstructQueue(people);
ps->printTest(result);
system("pause");
return 0;
}