# The Skyline Problem

This problem is a little hard. Given a series of buildings, we reserve a queue where in the queue the buildings is sorted by their right sides' position and their height is descending all the way. Note that the won't be a building in the queue contains right top point of another building in the queue.

We we are going to add next building, first from the head of queue we poll all buildings whose right side is on the left of left side of current building as these buildings won't affect skyline after current building's left side. Then we removes all buildings in the queue whose top right point is contained by current building. At last we insert current building to a proper position in the queue.

To build the result, we add upstair points when we first meet a buildings if it is higher than any buildings in the queue. Noted that many buildings may have the same left side position, so when adding upstair points, we first check last points and make sure the buildings with same left side position only contribute one highest upstair point in the result. We add downstair points when poll bulidings whose right side is at the left of current building as well as in the end of program to empty queue.

There are two ways to implement this algorithm. The first is using LinkedList and insertion method when adding items into the queue. The other method is using a PriorityQueue and let the PriorityQueue to sort buildings for us.

The LinkedList solution:

import java.util.*;

public class TheSkylineProblem {
public List<int[]> getSkyline(int[][] buildings) {
ArrayList<int[]> result = new ArrayList<int[]>();
if (buildings.length == 0 || buildings.length == 0) return result;

for (int i = 0; i < buildings.length; i++) {
int l = buildings[i], r = buildings[i], h = buildings[i];

while (!queue.isEmpty() && buildings[queue.getFirst()] < l) {
int right = buildings[queue.pollFirst()];
int height = queue.isEmpty() ? 0 : buildings[queue.getFirst()];
int[] newpoint = {right, height};
}

if (queue.isEmpty() || buildings[queue.getFirst()] < h) {

if (!result.isEmpty() && result.get(result.size() - 1) == l) {
int[] last = result.get(result.size() - 1);
last = Math.max(last, h);
} else {
int[] newpoint = {l, h};
}
}

ListIterator<Integer> iter = queue.listIterator();
while (iter.hasNext()) {
int index = iter.next();
if (buildings[index] <= r && buildings[index] <= h) {
iter.remove();
}
}

else {
iter = queue.listIterator();
while (iter.hasNext()) {
int index = iter.next();
if (buildings[index] >= r && buildings[index] >= h) break;

if (buildings[index] < h) {
iter.set(i);
break;
}

if (!iter.hasNext()) {
break;
}
}
}
}

while (!queue.isEmpty()) {
int right = buildings[queue.pollFirst()];
int height = queue.isEmpty() ? 0 : buildings[queue.getFirst()];
int[] newpoint = {right, height};
}

return result;
}
}


The PriorityQueue solution:

import java.util.*;

// Solve the skyline problem with priority queue

public class TheSkylineProblemII {
public List<int[]> getSkyline(int[][] buildings) {
ArrayList<int[]> result = new ArrayList<int[]>();
if (buildings.length == 0 || buildings.length == 0) return result;

final int[][] innerBuildings = buildings;

PriorityQueue<Integer> queue = new PriorityQueue<Integer>(
buildings.length,
new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return innerBuildings[a] - innerBuildings[b];
}
});

for (int i = 0; i < buildings.length; i++) {
int l = buildings[i], r = buildings[i], h = buildings[i];

while (!queue.isEmpty() && buildings[queue.peek()] < l) {
int right = buildings[queue.poll()];
int height = queue.isEmpty() ? 0 : buildings[queue.peek()];
int[] newpoint = {right, height};
}

if (queue.isEmpty() || buildings[queue.peek()] < h) {

if (!result.isEmpty() && result.get(result.size() - 1) == l) {
int[] last = result.get(result.size() - 1);
last = Math.max(last, h);
} else {
int[] newpoint = {l, h};
}
}

Iterator<Integer> iter = queue.iterator();

boolean flag = true;
while (iter.hasNext()) {
int index = iter.next();
if (buildings[index] <= r && buildings[index] <= h) {
iter.remove();
continue;
}

if (buildings[index] >= r && buildings[index] >= h) {
flag = false;
}
}

if (flag) queue.offer(i);
}

while (!queue.isEmpty()) {
int right = buildings[queue.poll()];
int height = queue.isEmpty() ? 0 : buildings[queue.peek()];
int[] newpoint = {right, height};