https://leetcode.com/problems/queue-reconstruction-by-height/
https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution
https://discuss.leetcode.com/topic/60423/java-o-n-2-greedy-solution
X. Using PriorityQueue
X. https://discuss.leetcode.com/topic/60550/o-n-sqrt-n-solution/13
https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers
(h, k)
, where h
is the height of the person and k
is the number of people in front of this person who have a height greater than or equal to h
. Write an algorithm to reconstruct the queue.
Note:
The number of people is less than 1,100.
The number of people is less than 1,100.
Example
Input: [[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]]
https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution
- Pick out tallest group of people and sort them in a subarray (S). Since there's no other groups of people taller than them, therefore each guy's index will be just as same as his k value.
- For 2nd tallest group (and the rest), insert each one of them into (S) by k value. So on and so forth.
E.g.
input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
subarray after step 1: [[7,0], [7,1]]
subarray after step 2: [[7,0], [6,1], [7,1]]
input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
subarray after step 1: [[7,0], [7,1]]
subarray after step 2: [[7,0], [6,1], [7,1]]
public int[][] reconstructQueue(int[][] people) {
//pick up the tallest guy first
//when insert the next tall guy, just need to insert him into kth position
//repeat until all people are inserted into list
Arrays.sort(people,new Comparator<int[]>(){
@Override
public int compare(int[] o1, int[] o2){
return o1[0]!=o2[0]?-o1[0]+o2[0]:o1[1]-o2[1];
}
});
List<int[]> res = new LinkedList<>();
for(int[] cur : people){
if(cur[1]>=res.size())
res.add(cur);
else
res.add(cur[1],cur);
}
return res.toArray(new int[people.length][]);
}
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
List<int[]> list = new LinkedList<>();
for (int[] p : people) {
list.add(p[1], p);
}
return list.toArray(new int[list.size()][]);
}
https://discuss.leetcode.com/topic/60981/explanation-of-the-neat-sort-insert-solution
Below is my explanation of the following neat solution where we sort people from tall to short (and by increasing k-value) and then just insert them into the queue using their k-value as the queue index:
def reconstructQueue(self, people):
people.sort(key=lambda (h, k): (-h, k))
queue = []
for p in people:
queue.insert(p[1], p)
return queue
I didn't come up with that myself, but here's my own explanation of it, as I haven't seen anybody explain it (and was asked to explain it):
People are only counting (in their k-value) taller or equal-height others standing in front of them. So a smallest person is completely irrelevant for all taller ones. And of all smallest people, the one standing most in the back is even completely irrelevant for everybodyelse. Nobody is counting that person. So we can first arrange everybody else, ignoring that one person. And then just insert that person appropriately. Now note that while this person is irrelevant for everybody else, everybody else is relevant for this person - this person counts exactly everybody in front of them. So their count-value tells you exactly the index they must be standing.
So you can first solve the sub-problem with all but that one person and then just insert that person appropriately. And you can solve that sub-problem the same way, first solving the sub-sub-problem with all but the last-smallest person of the subproblem. And so on. The base case is when you have the sub-...-sub-problem of zero people. You're then inserting the people in the reverse order, i.e., that overall last-smallest person in the very end and thus the first-tallest person in the very beginning. That's what the above solution does, Sorting the people from the first-tallest to the last-smallest, and inserting them one by one as appropriate.
We always choose the current shortest height (so we need to sort input first), and then try to put it into the right position. We simply scan from the left and count how many persons are really >= its own height. Then we put the person into the empty slot.
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people,new Comparator<int[]>(){
public int compare(int[] p1, int[] p2){
return p1[0]!=p2[0]?Integer.compare(p2[0],p1[0]): Integer.compare(p1[1],p2[1]);
}
});
List<int[]> list = new LinkedList(); // use arraylist or array
for (int[] ppl: people) list.add(ppl[1], ppl);
return list.toArray(new int[people.length][] );
}
https://discuss.leetcode.com/topic/60417/java-solution-using-arrays-sort-and-insert-sorting-ideaX. Using PriorityQueue
首先选出k值为0且身高最低的人,记为hi, ki,将其加入结果集。
然后更新队列,若队列中人员的身高≤hi,则令其k值 - 1(需要记录原始的k值)。
循环直到队列为空。
public int[][] reconstructQueue(int[][] people) {
int size = people.length;
LinkedList<int[]> list = new LinkedList<int[]>();
for (int i = 0; i < size; i++) {
list.add(new int[]{people[i][0], people[i][1], 0});
}
int ans[][] = new int[size][];
for (int i = 0; i < size; i++) {
Collections.sort(list, new Comparator<int[]>() {
public int compare (int[] a, int[] b) {
if (a[1] == b[1])
return a[0] - b[0];
return a[1] - b[1];
}
});
int[] head = list.removeFirst();
ans[i] = new int[]{head[0], head[1] + head[2]};
for (int[] p : list) {
if (p[0] <= head[0]) {
p[1] -= 1;
p[2] += 1;
}
}
}
return ans;
}X. https://discuss.leetcode.com/topic/60550/o-n-sqrt-n-solution/13
https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist
No comments:
Post a Comment