forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue Reconstruction by Height.cpp
42 lines (38 loc) · 1.17 KB
/
Queue Reconstruction by Height.cpp
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
// Runtime: 33 ms (Top 97.97%) | Memory: 12 MB (Top 60.85%)
#include <bits/stdc++.h>
using namespace std;
struct node {
int h, k;
node *next;
node(int h, int k) : h(h), k(k), next(nullptr){}
};
bool cmp(vector<int>& p1, vector<int>& p2) {
return (p1[0] != p2[0]) ? (p1[0] > p2[0]) : (p1[1] < p2[1]);
}
void insert(vector<int>& p, node **target) {
node *new_p = new node(p[0], p[1]);
new_p->next = (*target)->next;
(*target)->next = new_p;
}
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), cmp);
vector<int>& first = people[0];
node *head = new node(0, 0);//pseu
node **target;
int step;
for (int i = 0; i < people.size(); i++) {
vector<int>& p = people[i];
for (target = &head, step=p[1]; step > 0; target=&((*target)->next), step--);
insert(p, target);
}
vector<vector<int>> ans;
ans.resize(people.size());
int i = 0;
for(node *cur = head->next; cur != nullptr; cur = cur->next)
ans[i++] = vector<int>({cur->h, cur->k});
//formally, we should free the list later.
return ans;
}
};