-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueueReconstructionByHeight.h
46 lines (39 loc) · 1.38 KB
/
QueueReconstructionByHeight.h
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
//
// QueueReconstructionByHeight.h
//
//
// https://leetcode.com/problems/queue-reconstruction-by-height/description/
//
#ifndef QueueReconstructionByHeight_h
#define QueueReconstructionByHeight_h
class Solution {
public:
vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
map<int, vector<int>> hashMap;
vector<pair<int, int>> result(people.size(), pair<int, int>(-1, -1));
for (auto person : people) {
hashMap[person.first].push_back(person.second);
}
for (auto it = hashMap.begin(); it != hashMap.end(); it++) {
vector<int> tempNumbers = it->second;
sort(tempNumbers.begin(), tempNumbers.end());
for (int number : tempNumbers) {
int count = 0;
for (int i = 0; i < result.size(); i++) {
if (result[i].first == -1) {
if (count == number) {
result[i] = make_pair(it->first, number);
break;
} else {
count++;
}
} else if (result[i].first >= it->first) {
count++;
}
}
}
}
return result;
}
};
#endif /* QueueReconstructionByHeight_h */