-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
65 lines (57 loc) · 1.77 KB
/
solution.cpp
File metadata and controls
65 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Created by mingyi on 19.04.20.
//
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
// insert lowest first, fast
vector<vector<int>> reconstructQueueLowestFirst(vector<vector<int>> &people) {
sort(people.begin(), people.end(), [](const auto &p1, const auto &p2) {
if (p1[0] == p2[0]) {
return p1[1] > p2[1];
} else {
return p1[0] < p2[0];
}
// return p1[0] < p2[0] || (p1[0] == p2[0] && p1[1] > p2[1]); // this one line return is slower than the above one
});
int size = people.size();
vector<int> pos;
for (int i = 0; i < size; i++) {
pos.push_back(i);
}
vector<vector<int>> ret(size);
for (int i = 0; i < size; i++) {
int k = people[i][1];
ret[pos[k]] = people[i];
pos.erase(pos.begin() + k);
}
return ret;
}
// insert highest first, slow
vector<vector<int>> reconstructQueueHighestFirst(vector<vector<int>> &people) {
sort(people.begin(), people.end(), [](const auto &p1, const auto &p2) {
if (p1[0] == p2[0]) {
return p1[1] < p2[1];
} else {
return p1[0] > p2[0];
}
// return p1[0] > p2[0] || (p1[0] == p2[0] && p1[1] < p2[1]);
});
vector<vector<int>> ans;
for (const auto &p : people) {
ans.insert(ans.cbegin() + p[1], p); // this is slow, every insertion is O(n) search and O(n) copy
}
return ans;
}
int main() {
vector<vector<int>> people {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}};
vector<vector<int>> expect {{5, 0}, {7, 0}, {5, 2}, {6, 1}, {4, 4}, {7, 1}};
// vector<vector<int>> result = reconstructQueueHighestFirst(people);
vector<vector<int>> result = reconstructQueueLowestFirst(people);
for (int i = 0; i < result.size(); i++) {
assert(expect[i][0] == result[i][0]);
assert(expect[i][1] == result[i][1]);
}
return 0;
}