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){
res.add(cur[1],cur);
}
return res.toArray(new int[people.length][]);
}
Java 8
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people, (a, b) -> a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]);
List<int[]> res = new LinkedList<>();
for(int[] e : people){
res.add(e[1], e);
}
return res.toArray(new int[people.length][]);
}
https://leetcode.com/problems/queue-reconstruction-by-height/
https://leetcode.com/problems/merge-intervals/discuss/
Priority Queue and maxheap
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQueueTest {
static class PQsort implements Comparator<Integer> {
public int compare(Integer one, Integer two) {
return two - one;
}
}
public static void main(String[] args) {
int[] ia = { 1, 10, 5, 3, 4, 7, 6, 9, 8 };
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();
// use offer() method to add elements to the PriorityQueue pq1
for (int x : ia) {
pq1.offer(x);
}
System.out.println("pq1: " + pq1);
PQsort pqs = new PQsort();
PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs);
// In this particular case, we can simply use Collections.reverseOrder()
// instead of self-defined comparator
for (int x : ia) {
pq2.offer(x);
}
System.out.println("pq2: " + pq2);
// print size
System.out.println("size: " + pq2.size());
// return highest priority element in the queue without removing it
System.out.println("peek: " + pq2.peek());
// print size
System.out.println("size: " + pq2.size());
// return highest priority element and removes it from the queue
System.out.println("poll: " + pq2.poll());
// print size
System.out.println("size: " + pq2.size());
System.out.print("pq2: " + pq2);
}
}
Output:
pq1: [1, 3, 5, 8, 4, 7, 6, 10, 9]
pq2: [10, 9, 7, 8, 3, 5, 6, 1, 4]
size: 9
peek: 10
size: 9
poll: 10
size: 8
pq2: [9, 8, 7, 4, 3, 5, 6, 1]
http://www.programcreek.com/2009/02/using-the-priorityqueue-class-example/
TreeMap:
import java.util.*;
public class TreeMapDemo {
public static void main(String args[]) {
// Create a hash map
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put("Zara", new Double(3434.34));
tm.put("Mahnaz", new Double(123.22));
tm.put("Ayan", new Double(1378.00));
tm.put("Daisy", new Double(99.22));
tm.put("Qadir", new Double(-19.08));
// Get a set of the entries
Set set = tm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into Zara's account
double balance = ((Double)tm.get("Zara")).doubleValue();
tm.put("Zara", new Double(balance + 1000));
System.out.println("Zara's new balance: " + tm.get("Zara"));
}
}
Output:
Ayan: 1378.0
Daisy: 99.22
Mahnaz: 123.22
Qadir: -19.08
Zara: 3434.34
Zara's new balance: 4434.34
java.util.TreeMap.pollLastEntry() and java.util.TreeMap.pollFirstEntry()
package com.tutorialspoint;
import java.util.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
// polling last entry
System.out.println("Value before poll: "+ treemap);
System.out.println("Value returned: "+ treemap.pollLastEntry());
System.out.println("Value returned: "+ treemap.pollFirstEntry());
System.out.println("Value after poll: "+ treemap);
}
}
Output:
Value before poll: {1=one, 2=two, 3=three, 5=five, 6=six}
Value returned: 6=six
Value returned: 1=one
Value after poll: {1=one, 2=two, 3=three, 5=five}
https://www.tutorialspoint.com/java/java_treemap_class.htm
https://www.tutorialspoint.com/java/util/treemap_polllastentry.htm