You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).

&nbsp;
Example 1:

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.


Example 2:

Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]


&nbsp;
Constraints:


	1 &lt;= people.length &lt;= 2000
	0 &lt;= hi &lt;= 106
	0 &lt;= ki &lt; people.length
	It is guaranteed that the queue can be reconstructed.


class Solution:
	def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
		n = len(people)
		people.sort()
		ans = [[]]*n
		i = 0
		while people:
			h,p = people.pop(0)
			count= p
			for i in range(n):
				if count== 0 and ans[i] == []:
					ans[i] = [h,p]
					break

				elif not ans[i] or (ans[i] and ans[i][0] >= h ):
					count -= 1
		return ans
class Solution {
    public int[][] reconstructQueue(int[][] people) {
        List<int[]> result = new ArrayList<>(); //return value

        Arrays.sort(people, (a, b) -> {
            int x = Integer.compare(b[0], a[0]);
            if(x == 0) return Integer.compare(a[1], b[1]);
            else return x; });

        for(int[] p: people)
            result.add(p[1], p);

        return result.toArray(new int[people.length][2]);
    }
}
#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;
    }
};
var reconstructQueue = function(people) {
    var queue = new Array(people.length);
    people = people.sort((a,b) => (a[0]-b[0]));
    for(let i =0;i<people.length;i++){
        let count = 0;
        for(let j= 0;j<queue.length;j++){
            if(!queue[j]){
                if(count == people[i][1]){
                    queue[j] = people[i];
                    break;
                }
                count++;
            }
            else if( queue[j][0] >= people[i][0]){
                count++;
            }
        }
    }
    return queue;
};
Queue Reconstruction by Height