Skip to content

Instantly share code, notes, and snippets.

@Caesar-Victory
Created October 22, 2022 13:21
Show Gist options
  • Save Caesar-Victory/01f44efb64e74c1e5e83a501dfe6d5c1 to your computer and use it in GitHub Desktop.
Save Caesar-Victory/01f44efb64e74c1e5e83a501dfe6d5c1 to your computer and use it in GitHub Desktop.
#C++
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
// 多值排序,使用匿名函数 [](){} 没有不能访问的变量,形参; 排序规则:身高递增(数组位序0)或者 (身高相等排序人数(数组位序1),递减顺序)
sort(people.begin(), people.end(), [](const vector<int>& u, const vector<int>& v) {
return u[0] < v[0] || (u[0] == v[0] && u[1] > v[1]);
});
int n = people.size();
vector<vector<int>> ans(n);
for (const vector<int>& person: people) {
int spaces = person[1] + 1;
for (int i = 0; i < n; i++) {
if (ans[i].empty()) {
spaces--;
if (!spaces) {
ans[i] = person;
break;
}
}
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment