..

Qriority Queue, The Skyline Problem

Problem

218. The Skyline Problem

A city’s skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively.

The geometric information of each building is given in the array buildings where buildings[i] = [lefti, righti, heighti]:

  • lefti is the x coordinate of the left edge of the ith building.
  • righti is the x coordinate of the right edge of the ith building.
  • heighti is the height of the ith building.

You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

The skyline should be represented as a list of “key points” sorted by their x-coordinate in the form [[x1,y1],[x2,y2],...]. Each key point is the left endpoint of some horizontal segment in the skyline except the last point in the list, which always has a y-coordinate 0 and is used to mark the skyline’s termination where the rightmost building ends. Any ground between the leftmost and rightmost buildings should be part of the skyline’s contour.

Note: There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...,[2 3],[4 5],[7 5],[11 5],[12 7],...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...,[2 3],[4 5],[12 7],...]

Constraints:

  • 1 <= buildings.length <= 104
  • 0 <= lefti < righti <= 231 - 1
  • 1 <= heighti <= 231 - 1
  • buildings is sorted by lefti in non-decreasing order.

문제 해석

My Approach

[Li, Ri, HI]의 데이터가 주어지면 지점들을 기록해서 X의 오름차순으로 정렬 하기 위해서 데이 터를 parsing 해주어야 한다.

문제 접근

우선 순위 큐(Priority Queue)를 활용

  • 최대, 최소값을 반환하고 싶을때 최대, 최소를 찾는 operation을 효율적으로 해준다 (log n)
  • 지점들을 포함
    • 포함 데이터: e.g., (2, 10) 빌딩 시작, (9, 10) 빌딩 끝
    • x에 대한 오름차순으로 가지고 있기
    • 사진을 X축을 기준으로 왼쪽에서 오른쪽으로 스캔을 한다고 생각하면 됨
  • 사건들이 있을 때 마다 skyline에 변동이 있었는지 확인
    • e.g., 빌딩이 새로 등장 or 기존의 빌딩이 끝날때
    • 변동이 있다면 기록
    • height이 높은 순으로 기록

Skyline(등고선)처리시 list정렬 조건

case 1) 한 점은 start고 다른 하나는 end일 경우 a.start && !b.start !a.start && b.start

  • end(끝나는 것)먼저 처리하게 되면 skyline이 0으로 변한다. 이것은 우리가 원하는게 아니다

case 2) start가 같을때 높은 빌딩부터 처리해야 한다

case 3) end가 같을때 낮은 빌딩부터 처리해야 한다

  • 오타 - 높은것 먼저 처리 (아래부분)
class Solution {
	public class Point {
		int x;
		int height;
		boolean start;
		public Point(int x, int height, boolean start) {
			this.x = x;
			this.height = height;
			this.start = start;
		}
	}

	public List<List<Integer>> getSkyline(int[][] buildings) {
		// building parsing
		List<Point> list = new ArrayList<>();
		for(int[] b : buildings){
			list.add(new Point(b[0], b[2], true));
			list.add(new Point(b[1], b[2], false));
		}

		// Sorting
		
		// PQ
		PriorityQueue<Integer> pq = new PriorityQueue<>(3, (a,b) -> (b->a)) // 내림차순
		pq.offer(0);   // 최후에는 0을 꼭 찍어 주어야 한다

		List<List<Integer>> ret = new ArrayList<>();
		for(Point p : list){
			int max = pq.peek();
			if(p.start){
				pq.offer(p.height);
			} else {
				pq.remove(p.height);
			}
			
			// skyline(등고선)에 변화가 있을때
			if(pq.peek() != max){
				Integer[] arr = new Integer[2];
				arr[0] = p.x;
				arr[1] = pq.peek();
				var temp = Arrays.stream(arr).collect(Collectors.toList());
				ret.add(temp);
				// ret.add(new int[]{p.x, pq.peek()})
			}
		}
		return ret;
	}
}