218.The-Skyline-Problem

218. The Skyline Problem

้ข˜็›ฎๅœฐๅ€

https://leetcode.com/problems/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. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

Buildings Skyline Contour
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 โ‰ค Li, Ri โ‰ค INT_MAX, 0 < Hi โ‰ค INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.

For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .

The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.

For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].

Notes:
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
The input list is already sorted in ascending order by the left x position Li.
The output list must be sorted by the x position.
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], ...]

ไปฃ็ 

Approach 1: Divide and Conquer

class Solution {
  public List<List<Integer>> getSkyline(int[][] buildings) {
        int n = buildings.length;
    List<List<Integer>> output = new ArrayList<List<Integer>>();

    if (n == 0)    return output;
    if (n == 1)    {
      int xStart = buildings[0][0];
      int xEnd = buildings[0][1];
      int y = buildings[0][2];

      output.add(new ArrayList<Integer>() {{ add(xStart); add(y); }});
      output.add(new ArrayList<Integer>() {{ add(xEnd); add(0); }});

      return output;
    }

    List<List<Integer>> lfetSkyline, rightSkyline;
    leftSkyline = getSkyline(Arrays.copyOfRange(buildings, 0, n / 2));
    rightSkyline = getSkyline(Arrays.copyOfRange(buildings, n / 2, n));

    return mergeSkylines(leftSkyline, rightSkyline);
  }

  public List<List<Integer>> mergeSkylines(List<List<Integer>> left, List<List<Integer>> right) {
    int nL = left.size(), nR = right.size();
    List<List<Integer>> output = new ArrayList<List<Integer>>();

    int x, maxY;
    int pL = 0, pR = 0;
    int currY = 0, leftY = 0, rightY = 0;
    while ((pL < nL) && (pR < nR)) {
      List<Integer> pointL = left.get(pL);
      List<Integer> pointR = right.get(pR);
      // pick up the smallest x
      if (pointL.get(0) < pointR.get(0)) {
        x = pointL.get(0);
        leftY = pointL.get(1);
        pL++;
      } else {
        x = pointR.get(0);
        righY = pointR.get(1);
        pR++;
      }
      maxY = Math.max(leftY, rightY);
      // update output if there is a skyline change
      if (currY != maxY) {
        updateOutput(output, x, maxY);
        currY = maxY;
      }
    }

    appendSkyline(output, left, pL, nL, currY);
    appendSkyline(output, right, pR, nR, currY);

    return output;
  }

  public void updateOutput(List<List<Integer>> output, int x, int y) {
     // if skyline change is not vertical - add the new point
    if (output.isEmpty() || output.get(output.size() - 1).get(0) != x) {
      output.add(new ArrayList<Integer>() {{ add(x); add(y); }}); 
    } else {
    // if skyline change is vertical - update the last point
      output.get(output.size() - 1).set(1, y);
    }
  }

/**
 *  Append the rest of the skyline elements with indice (p, n)
 *  to the final output.
 */
  public void appendSkyline(List<List<Integer>> output, List<List<Integer>> skyline, int p, int n, int currY) {
    while (p < n) {
      List<Integer> point = skyline.get(p);
      int x = point.get(0);
      int y = point.get(1);
      p++;
      // update output if there is a skyline change
      if (currY != y) {
        updateOutput(output, x, y);
        currY = y;
      }
    }
  }

}

Approach #2 Sweep line

ๆœฌ้ข˜ๆ˜ฏ็ปๅ…ธ็š„sweep line้—ฎ้ข˜ใ€‚

ๅฏนไบŽsweep line้—ฎ้ข˜ๆˆ‘ไปฌ้œ€่ฆ่€ƒ่™‘็š„ๅชๆœ‰ไธค็‚น๏ผš

1. ๅปถๆฐดๅนณๆ–นๅ‘ / ๆ—ถ้—ดๆ–นๅ‘ ๏ผšๆ—ถ้—ด้˜Ÿๅˆ— event queue๏ผŒไธ€่ˆฌๆฅ่ฏดๆ˜ฏไธ€ไธชไผ˜ๅ…ˆ้˜Ÿๅˆ—๏ผ›

2. ๅปถๅž‚็›ดๆ–นๅ‘ ๏ผšsweep line status๏ผŒๅณๅฝ“ๅ‰็š„ๆ‰ซๆ็บฟ็š„็Šถๆ€๏ผŒไธ€่ˆฌไผšๅฐ†ไบค็‚นๆŒ‰็…ง้กบๅบๆŽ’ๅบ๏ผ›

ๅฏนไบŽๆœฌ้ข˜ๆฅ่ฏด๏ผŒsweep line statusๅฏไปฅไฝฟ็”จไธ€ไธชmulti setๆฅ่ฟ›่กŒ็ปดๆŠค๏ผŒๅฝ“็„ถ็”ฑไบŽๅœจJavaไธญๆฒกๆœ‰multi set๏ผŒๅ› ๆญค้œ€่ฆไฝฟ็”จTreeMapๆฅๆจกๆ‹Ÿใ€‚

event queue็š„ๅฝ“็„ถๆ˜ฏไฝฟ็”จไผ˜ๅ…ˆ้˜Ÿๅˆ—๏ผŒ้—ฎ้ข˜ๆ˜ฏๅฆ‚ไฝ•่ฟ›่กŒๆŽ’ๅบ๏ผŒ่ฟ™ไธชๆ‰ๆ˜ฏๆœฌ้ข˜็š„ๆ ธๅฟƒ้šพ็‚นใ€‚

่ฟ™้‡Œ็ป™ๅ‡บ็ป“่ฎบ๏ผš

ๅคงๆ–นๅ‘ๆ˜ฏๆŒ‰็…งx่ฝดๆŽ’ๅบ๏ผŒๅฆ‚ๆžœx่ฝด็›ธ็ญ‰้‚ฃไนˆๆŒ‰็…งheightๆŽ’ๅบ๏ผ›

ๅฆ‚ๆžœx่ฝด็›ธ็ญ‰๏ผŒไผ˜ๅ…ˆๅˆคๆ–ญๆ˜ฏๅฆๆ˜ฏๅทฆ็ซฏ็‚น๏ผŒๅฆ‚ๆžœๆ˜ฏๅทฆ็ซฏ็‚น๏ผŒ้‚ฃไนˆไผ˜ๅ…ˆๅ…ฅ้˜Ÿ๏ผ›

ๅฆ‚ๆžœๅŒๆ—ถๆ˜ฏๅณ็ซฏ็‚น๏ผŒ้‚ฃไนˆ้œ€่ฆๅๅบๅ…ฅ้˜Ÿ๏ผŒๅฐฑๆ˜ฏheightๅฐ็š„ๅ่€Œ้œ€่ฆๆŽ’ๅœจๅ‰้ขใ€‚

public List<List<Integer>> getSkyline(int[][] buildings) {
    List<List<Integer>> res = new ArrayList<>();
    // ๅฏนไบŽๅŒx่ฝด๏ผŒไผ˜ๅ…ˆๅฐ†ๅทฆ็ซฏ็‚นๅ…ฅ้˜Ÿๅˆ—
    // ๅฆ‚ๆžœๅŒๆ˜ฏๅณ็ซฏ็‚น๏ผŒๅˆ™่ฆๅๅบ๏ผŒyๅฐ็š„ๅ…ˆๅ…ฅ้˜Ÿๅˆ—
    // ๅ…ถไฝ™ๆŒ‰็…งๆญฃๅธธ็š„height้กบๅบๆŽ’ๅˆ—ๅณๅฏ
    PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){
        public int compare(int[] o1, int[] o2) {
            if (o1[0] == o2[0] && o1[2] != o2[2]) return o1[2] - o2[2];
            if (o1[0] == o2[0] && o1[2] == 1 && o2[2] == 1) return o1[1] - o2[1];
            return o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0];
        }
    });
    TreeMap<Integer, Integer> map = new TreeMap<>();
    for (int[] b : buildings) {
        int s = b[0];
        int e = b[1];
        int h = b[2];
        pq.add(new int[]{s, h, 0});
        pq.add(new int[]{e, h, 1});
    }
    while (!pq.isEmpty()) {
        int[] event = pq.poll();
        int x = event[0];
        int h = event[1];
        int status = event[2];        
        int curr_max = map.size() == 0 ? 0 : map.lastKey();
        if (status == 0) {
            if (h > curr_max) res.add(Arrays.asList(x, h));
            map.put(h, map.getOrDefault(h, 0) + 1);
        } 
        else {
            map.put(h, map.get(h) - 1);
            if (map.get(h) == 0) map.remove(h);
            curr_max = map.size() == 0 ? 0 : map.lastKey();
            if (h > curr_max) res.add(Arrays.asList(x, curr_max));
        }

    }
    return res;
}

Last updated