Skip to content

Commit 52b66a1

Browse files
committed
leetcode june challenge
1 parent 1267346 commit 52b66a1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
using namespace std;
5+
class Solution
6+
{
7+
static bool comparator(vector<int> &a, vector<int> &b)
8+
{
9+
if (a[0] == b[0])
10+
return a[1] < b[1];
11+
return a[0] < b[0];
12+
}
13+
14+
public:
15+
vector<vector<int>> reconstructQueue(vector<vector<int>> &people)
16+
{
17+
std::ios::sync_with_stdio(false);
18+
cin.tie(NULL);
19+
cout.tie(NULL);
20+
int n = people.size();
21+
sort(people.begin(), people.end(), comparator);
22+
vector<vector<int>> ans(n, vector<int>(2, -1));
23+
for (int i = 0; i < n; i++)
24+
{
25+
int count = people[i][1];
26+
for (int j = 0; j < n; j++)
27+
{
28+
if (ans[j][0] == -1 && count == 0)
29+
{
30+
ans[j][0] = people[i][0];
31+
ans[j][1] = people[i][1];
32+
break;
33+
}
34+
else if (ans[j][0] == -1 || ans[j][0] >= people[i][0])
35+
{
36+
count -= 1;
37+
}
38+
}
39+
}
40+
return ans;
41+
}
42+
};

0 commit comments

Comments
 (0)