LeetCode 218. The Skyline Problem--Divide and Conquer(分治法)

来源:互联网 发布:oracle创建数据库语句 编辑:程序博客网 时间:2024/05/22 04:52

题目链接

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. 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).


           


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_MAX0 < 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], ...]


解:题目很复杂,最初看起来可以说是毫无头绪,根据题目要求,如果只有一个建筑,那么输出的点一定是建筑的左上顶点和右下顶点,根据这个思路,想到用分治法来解决,每次将buildings一分为二做递归,当buildings的数量为1时,返回2个点,那么难点在于合并两组点集,并且要始终保持点集有序。

合并点集时,和归并排序的思路有点相似,分别使用2个指针(int值)指向当前left和right的位置,每次选择横坐标较小的点插入结果中,保证结果有序。结果点的横坐标等于原横坐标,至于结果点的纵坐标,应该是取right和left中的较大值,而这个高度的值也应有2个变量实时记录,代表的含义是left和right当前的高度,为了更好的说明,如下图:


画圈的点是left中的一个点,在合并时,因为点的高度小于right的高度,结果中点的纵坐标就是right的高度。

合并时还需要注意的两点:

1.合并的第一个点可以无条件插入到结果中,当left或者right全部遍历后,一方的剩余点不再需要比较直接插入结果中

2.点插入到结果中时,应注意点的高度一定不能和上一个点相同,如果多个连续的点纵坐标相同,则结果中只能有第一个点(结果中不会出现横坐标相等的点,也不会出现连续的纵坐标相等的点)

代码:

class Solution {public:    vector<pair<int, int> > merge(vector<pair<int, int> >& left, vector<pair<int, int> >& right) {    int l = 0, r = 0;    int lheight = 0, rheight = 0;    vector<pair<int, int> > temp;    while (l < left.size() && r < right.size()) {    if (left[l].first == right[r].first) {    lheight = left[l].second;    rheight = right[r].second;    if (temp.empty() || temp.back().second != max(lheight, rheight))     temp.push_back(make_pair(left[l].first, max(lheight, rheight)));    l++;    r++;        }    else if(left[l].first < right[r].first) {    lheight = left[l].second;    if (temp.empty() || temp.back().second != max(lheight, rheight))     temp.push_back(make_pair(left[l].first, max(lheight, rheight)));    l++;    } else{    rheight = right[r].second;    if (temp.empty() || temp.back().second != max(lheight, rheight))     temp.push_back(make_pair(right[r].first, max(lheight, rheight)));        r++;    }    }    for(int i = l; i < left.size(); i++)            if(temp.empty() || temp.back().second != left[i].second) temp.push_back(left[i]);        for(int i = r; i < right.size(); i++)            if(temp.empty() || temp.back().second != right[i].second) temp.push_back(right[i]);    return temp;    }    vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {        int size = buildings.size();        if (size == 0) return {};        if (size == 1) {        vector<pair<int, int> > temp;        temp.push_back(make_pair(buildings[0][0], buildings[0][2]));        temp.push_back(make_pair(buildings[0][1], 0));        return temp;        }        vector<vector<int> > buildings1(buildings.begin(), buildings.begin()+size/2);        vector<vector<int> > buildings2(buildings.begin()+size/2, buildings.end());        vector<pair<int, int> > left = getSkyline(buildings1);        vector<pair<int, int> > right = getSkyline(buildings2);        return merge(left, right);    }};



原创粉丝点击