Leetcode-The Skyline Problem题解

一. 题目 Leetcode平台上天际线问题

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

二.解题思路

萌新在刚刚看到这道题的时候表示是无从下手的,后来在看时辰巨巨的博客时了解到了线段树这个数据结构,感觉这个数据结构真是很牛逼啊,也很适合解决这道题~

首先贴上一个讲解线段树的博客,也是从时辰的博客里看到的,感觉对线段树的讲解的确很生动形象,样例给的也很有启发性。

线段树博客

就像上文博客中作者提到的:

线段树的每个节点上往往都增加了一些其他的域。在这些域中保存了某种动态维护的信息,视不同情况而定。这些域使得线段树具有极大的灵活性,可以适应不同的需求。

这段话体现了线段树灵活性的好处,为了适应这道题的要求,我设计了这样的线段树数据结构:

typedef struct SegmentNode
{
	int start;      //线段树的区间左端点
	int end;        //线段树的区间有端点
	int isFlat;      //当前区间内的天际线是否是水平的
	int height;      //如果isFlat等于1,代表当前区间天际线的高度,否则无意义
	SegmentNode* left;  //左儿子
	SegmentNode* right;  //右儿子
}SegmentNode;

  解题思路是这样的,初始时天际线是一条高度为0的水平线,每添加一个矩形(对应线段树插入节点的操作)就更新天际线。

  假设已经有了一条天际线,那么在此基础上添加一个矩形时,对于线段树的某个端点,若其所代表的区间包含新添加矩形所在区间,天际线的变化将会有以下几种可能的情况:

  1. 原有的天际线是水平的

      这时如果矩形高度小于等于原有高度,那么天际线不会发生变化

      如果矩形高度大于原有高度,如果矩形所在区间恰好等于该线段树节点所代表的区间,那么直接更新该节点的height值为矩形高度。如果矩形所在区间小于等于该线段树节点所在区间,那么将该节点的isFlat值重置为0,对该端点左右子树进行递归,直至找到恰好等于矩形所在区间的线段树端点为止,这里需要注意的一点是如果矩形和左右节点所代表的区间都有重合的部分,那么需要将矩形分裂为两个矩形进行操作。

  2. 原有的天际线不是水平的,那么对该端点左右子树递归,直至找到isFlat=1的端点为止。

以上过程都是在插入线段树节点过程中进行的,插入节点函数的代码如下:

void insertNode(int l, int r, int h, SegmentNode* tree)      //因为这个函数是递归调用的,所以可以保证矩形范围在对应节点区间内
{
	if(!tree)
		return;
	if(tree->isFlat)                        //当前该区间天际线水平
	{
		int mid = tree->start / 2 + tree->end / 2;
		if((tree->start % 2) && (tree->end % 2))
			mid++;                    //求该区间的中间节点
		if(h <= tree->height)                //当前该区间天际线高度大于矩形高度
			return;
		if(tree->start == l && tree->end == r)      //当前区间即是矩形区间
		{
			tree->height = h;
			return;
		}
		else if(mid >= r)                  //矩形全部都在左子树代表的区间中
		{
			tree->isFlat = 0;
			if(!tree->left)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->right->height = tree->height;
				tree->left->height = tree->height;
			}
			insertNode(l, r, h, tree->left);
			//insertNode(mid, tree->end, tree->height, tree->right);
		}
		else if(mid <= l)                            //矩形全部都在右子树对应区间中
		{
			tree->isFlat = 0;
			if(!tree->right)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->left->height = tree->height;
				tree->right->height = tree->height;
			}
			insertNode(l, r, h, tree->right);
		}
		else                                      //矩形横跨左右子树区间
		{
			tree->isFlat = 0;
			if(!tree->left)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->left->height = tree->height;
				tree->right->height = tree->height;
			}
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
	}
	else                                          //当前线段树不平
	{
		int mid = tree->start / 2 + tree->end / 2;
		if((tree->start % 2) && (tree->end % 2))
			mid++;
		if(tree->start == l && tree->end == r)
		{
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
		else if(mid >= r)
		{
			insertNode(l, r, h, tree->left);
		}
		else if(mid <= l)
		{
			insertNode(l, r, h, tree->right);
		}
		else
		{
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
	}
}

  于是在添加完了所有的矩形之后,对线段树进行中序遍历,线段树的所有叶子节点储存的就是天际线的信息了。最后在Leetcode平台上提交的测试代码是这样的(因为之前线段树的代码部分有注释了,这里相应部分就不再写注释了):

typedef struct SegmentNode
{
	int start;
	int end;
	int isFlat;
	int height;
	SegmentNode* left;
	SegmentNode* right;
}SegmentNode;

SegmentNode* createTree(int leftRange, int rightRange)          //线段树的初始化
{
	SegmentNode* tree = new SegmentNode;
	tree->start = leftRange;
	tree->end = rightRange;
	tree->left = NULL;
	tree->right = NULL;
	tree->isFlat = 1;
	tree->height = 0;
	return tree;
}

void insertNode(int l, int r, int h, SegmentNode* tree)
{
	if(!tree)
		return;
	if(tree->isFlat)
	{
		int mid = tree->start / 2 + tree->end / 2;
		if((tree->start % 2) && (tree->end % 2))
			mid++;
		if(h <= tree->height)
			return;
		if(tree->start == l && tree->end == r)
		{
			tree->height = h;
			return;
		}
		else if(mid >= r)
		{
			tree->isFlat = 0;
			if(!tree->left)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->right->height = tree->height;
				tree->left->height = tree->height;
			}
			insertNode(l, r, h, tree->left);
			//insertNode(mid, tree->end, tree->height, tree->right);
		}
		else if(mid <= l)
		{
			tree->isFlat = 0;
			if(!tree->right)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->left->height = tree->height;
				tree->right->height = tree->height;
			}
			insertNode(l, r, h, tree->right);
		}
		else
		{
			tree->isFlat = 0;
			if(!tree->left)
			{
				tree->left = createTree(tree->start, mid);
				tree->right = createTree(mid, tree->end);
				tree->left->height = tree->height;
				tree->right->height = tree->height;
			}
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
	}
	else
	{
		int mid = tree->start / 2 + tree->end / 2;
		if((tree->start % 2) && (tree->end % 2))
			mid++;
		if(tree->start == l && tree->end == r)
		{
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
		else if(mid >= r)
		{
			insertNode(l, r, h, tree->left);
		}
		else if(mid <= l)
		{
			insertNode(l, r, h, tree->right);
		}
		else
		{
			insertNode(l, mid, h, tree->left);
			insertNode(mid, r, h, tree->right);
		}
	}
}

void getResult(SegmentNode* tree, vector<pair<int, int>>& result, int& lastHeight)
{
	if(!tree)
		return;
	pair<int, int> temp;
	if(tree->isFlat)                        //如果该节点对应区间是平坦的,得到结果,不然对左右子树进行递归
	{
		temp.first = tree->start;
		temp.second = tree->height;
		if(temp.second != lastHeight)            //如果该节点高度和上一个节点高度相等,说明天际线不发生弯折,就不输出这个节点信息了
		{
			result.push_back(temp);
		}
		lastHeight = temp.second;
		return;
	}
	else
	{
		getResult(tree->left, result, lastHeight);
		getResult(tree->right, result, lastHeight);
	}
}

class Solution {
public:
	vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
		int rightRange = 0;
		for(int i = 0; i < buildings.size(); i++)
		{
			if(buildings[i][1] > rightRange)
				rightRange = buildings[i][1];
		}                                                  //确定线段树根节点的区间范围
		SegmentNode* tree = createTree(0, rightRange);
		for(int i = 0; i < buildings.size(); i++)
		{
			insertNode(buildings[i][0], buildings[i][1], buildings[i][2], tree);      //逐个插入矩形,更新天际线
		}
		vector<pair<int, int>> result;
		int lastHeight = 0;
		int lastPos = 0;
		getResult(tree, result, lastHeight);                            //得到天际线
		pair<int, int> temp;
		temp.first = rightRange;
		temp.second = 0;
		if(rightRange)
			result.push_back(temp);                                //添加天际线的最后一个节点
		return result;
	}
};

  在提交过程中,出现了一些问题:一开始我在建立线段树时直接建立了完全二叉树,结果在测试样例[0, 2147483647, 2147483647]时出现了memory limit exceeded错误,后来改成在新建线段树中只新建根节点,在插入节点过程中再根据需要申请空间,问题得到了解决;还有一些各种各样的错误,基本都是因为自己脑残造成的。。。

最后的提交结果如图所示,可以看到运行速度还是挺快的,可能是因为没有使用STL的结果。

  

三. 总结

解这道题的过程还是很有收获的,主要体现在对线段树这个数据结构有了初步的了解,调试代码的过程也让我对自己可能会犯什么五花八门的错误有了新的认识,感觉姿势水平得到了提高。最后,如果你看到了我的这篇博客,希望我们共同学习,共同进步~

时间: 2024-10-15 16:06:38

Leetcode-The Skyline Problem题解的相关文章

[LeetCode] The Skyline Problem

An interesting problem! But not very easy at first glance. You need to think very clear about how will a keypoint be generated. Since I only learn from others' solutions and am still unable to give my personal explanations, I would suggest you to the

[LeetCode] 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 pr

解题报告:LeetCode The Skyline Problem(画天际

题目出处:https://leetcode.com/submissions/detail/47013144/题意描述: 给定一系列矩形的左边坐标Li,右边坐标Ri,和高度Hi(其中Li按照从小到大的顺序排列).代表城市中一座座高楼.求这些矩形代表的高楼行成的天际线.天际线的定义为:在远处看这些所有的高楼时看到的轮廓. 数据输入: [ [L1, R1, H1], [Li, Ri, Hi]...]为元素类型为三维向量的向量,其中Li,Ri,Hi的含义见题意描述.且输入数据保证: 0 ≤ Li, Ri

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

LeetCode: Add Two Numbers 题解

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】【Python题解】Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] 要求输入一个整数,返回一个表示杨辉三角的数组.我的方法是计算通项公式,首先是编写阶乘函数,然后计算C00,C10,C11即可 利用Python 的map嵌套可以很简洁地实现,核心代码只有一行! class So

LeetCode: Longest Common Prefix 题解

Write a function to find the longest common prefix string amongst an array of strings. 题解: 寻找一组字符串的最长公共前缀.  最简单的方法,用一个字符串记录当前最长的公共前缀,然后依次比较.时间复杂度: O(N). 1 class Solution { 2 public: 3 string getPrefix(string a,string b) // 辅助函数用于获取两个字符串的公共前缀 4 { 5 st

LeetCode: Count and Say 题解

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given an

LeetCode: Roman to Interger 题解

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 找到规则即可 罗马数字的表示: I~1 V~5 X~10 L~50 C~100 D~500 M~1000 规则: 基本数字Ⅰ.X .C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个:放在大数的左边只能用一个. 不能把基本数字V .L .D 中的任何一