public List<int[]> getSkyline(int[][] buildings) {
// Exceptions:
// _______ ________
// |___ | | __|
// | | | | | |
// _|___|_|__ _|___|__|_
//
List<int[]> buildingPoints = new ArrayList<>();
// trick to set negative on left edge
for (int[] building: buildings){
buildingPoints.add(new int[] {building[0], -building[2]});
buildingPoints.add(new int[] {building[1], building[2]});
}
Collections.sort(buildingPoints,
// because negative hight we got right sorting order
(a,b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]
);
List<int[]> ans = new ArrayList<>();
Queue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
// 0 is the compare base
maxHeap.add(0);
int preHeight = 0;
for (int[] point: buildingPoints){
if (point[1] < 0){
maxHeap.add(-point[1]);
}else{
maxHeap.remove(point[1]);
}
int maxHeight = maxHeap.peek();
if (preHeight != maxHeight){
ans.add(new int[] {point[0], maxHeight});
preHeight = maxHeight;
}
}
return ans;
}
Cheapest Price In Time Intervals
Given intervals of time and a lower price for the item during this interval, calculate the cheapest price for the item during the day within different time intervals. Input data is {startTime, endTime, price}. For example: [{0, 4, 5}, {2, 8, 3}, {7, 11, 10}], the result should be [{0, 2, 5}, {2, 8, 3}, {8, 11, 10}]”
Decouple input to entry and exit points
Maintain minHeap as available heights in current point
Update result whenever a hight change and modify the end time in pre interval