|
| 1 | +# 0005 Trapping Rain Water ( L-A ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +The geometric information of each building is given in the array `buildings` where `buildings[i] = [lefti, righti, heighti]`: |
| 8 | + |
| 9 | +- `lefti` is the x coordinate of the left edge of the ith building. |
| 10 | +- `righti` is the x coordinate of the right edge of the ith building. |
| 11 | +- `heighti` is the height of the ith building. |
| 12 | + |
| 13 | +You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0. |
| 14 | + |
| 15 | +## Example 1 |
| 16 | + |
| 17 | +``` |
| 18 | +Input: buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]] |
| 19 | +Output: [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]] |
| 20 | +Explanation: |
| 21 | +Figure A shows the buildings of the input. |
| 22 | +Figure B shows the skyline formed by those buildings. The red points in figure B represent the key points in the output list. |
| 23 | +``` |
| 24 | + |
| 25 | +## Solution Pseudocode |
| 26 | + |
| 27 | +```javascript |
| 28 | +Function getSkyline(buildings): |
| 29 | + // base case |
| 30 | + if (length(buildings) == 1): |
| 31 | + return [(buildings[0].left, buildings[0].height), (buildings[0].right, 0)] |
| 32 | + |
| 33 | + // divide the buildings into two groups |
| 34 | + mid = length(buildings) // 2 |
| 35 | + left = getSkyline(buildings[:mid]) |
| 36 | + right = getSkyline(buildings[mid:]) |
| 37 | + |
| 38 | + // merge the two groups |
| 39 | + return merge(left, right) |
| 40 | + |
| 41 | +Function merge(left, right): |
| 42 | + // initialize the pointers and the result array |
| 43 | + i, j = 0, 0 |
| 44 | + result = [] |
| 45 | + left_height, right_height = 0, 0 |
| 46 | + // merge the two lists |
| 47 | + while (i < len(left) and j < len(right)): |
| 48 | + if (left[i][0] < right[j][0]): |
| 49 | + x = left[i][0] |
| 50 | + left_height = left[i][1] |
| 51 | + height = max(left_height, right_height) |
| 52 | + result.append((x, height)) |
| 53 | + i += 1 |
| 54 | + else: |
| 55 | + x = right[j][0] |
| 56 | + right_height = right[j][1] |
| 57 | + height = max(left_height, right_height) |
| 58 | + result.append((x, height)) |
| 59 | + j += 1 |
| 60 | + // append the remaining points from left or right |
| 61 | + while (i < len(left)): |
| 62 | + result.append(left[i]) |
| 63 | + i += 1 |
| 64 | + while (j < len(right)): |
| 65 | + result.append(right[j]) |
| 66 | + j += 1 |
| 67 | + |
| 68 | + return result |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +## How it works |
| 73 | +- The `getSkyline` function is the main function that takes in a list of buildings as input and returns the skyline as a list of points. The function first checks if there is only one building in the list, in which case it returns two points representing the left and right boundaries of the building. |
| 74 | +- If there are more than one building in the list, the function recursively divides the buildings into two groups using the midpoint, and calls itself on each group. The results from the two recursive calls are then merged using the `merge` function. |
| 75 | +- The `merge` function takes in two lists of points representing the skylines of the left and right groups and merges them into a single list. The function initializes two pointers, `i` and `j`, to 0 and sets `left_height` and `right_height` to 0. The `result` array is used to store the merged skyline. |
| 76 | +- The function then uses a while loop to iterate over the two input lists. At each iteration, it compares the x-coordinates of the points at the current positions of the two pointers. If the x-coordinate of the point in the left list is less than the x-coordinate of the point in the right list, it means that the left building is closer to the viewer, so the function uses the left building's height to update the `left_height` variable and calculates the maximum height between `left_height` and `right_height`. The function then appends a new point with the x-coordinate and maximum height to the `result` array, and increments the `i` pointer. |
| 77 | +- If the x-coordinate of the point in the right list is less than or equal to the x-coordinate of the point in the left list, it means that the right building is closer to the viewer, so the function uses the right building's height to update the `right_height` variable |
| 78 | + |
| 79 | +## References |
| 80 | + |
| 81 | +- [LeetCode](https://leetcode.com/problems/the-skyline-problem/) |
| 82 | + |
| 83 | +## Problem Added By |
| 84 | + |
| 85 | +- [Haris](https://github.com/harisdev-netizen) |
| 86 | + |
| 87 | +## Contributing |
| 88 | + |
| 89 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 90 | + |
| 91 | +Please make sure to update tests as appropriate. |
0 commit comments