aboutsummaryrefslogtreecommitdiff
path: root/queue_reconstruction.cpp
blob: 17ea0ea64b526f2f399d1af8509ef775e3118005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "leetcode.h"

class Solution {
public:
  vector<vector<int>> reconstructQueue(vector<vector<int>> &people) {
    sort(people.begin(), people.end(), cmp);
    vector<vector<int>> ans;
    for (int i = 0; i < people.size(); i++)
      ans.insert(ans.begin() + people[i][1], people[i]);
    return ans;
  }

private:
  static bool cmp(vector<int> &p1, vector<int> &p2) {
    if (p1[0] != p2[0])
      return p1[0] > p2[0];
    else
      return p1[1] < p2[1];
  }
};