218. The Skyline Problem

class Edge {
	int x;
	int height;
	boolean isStart;

	public Edge(int x, int height, boolean isStart) {
		this.x = x;
		this.height = height;
		this.isStart = isStart;
	}
}

public List<int[]> getSkyline(int[][] buildings) {
	List<int[]> result = new ArrayList<int[]>();

	if (buildings == null || buildings.length == 0
			|| buildings[0].length == 0) {
		return result;
	}

	List<Edge> edges = new ArrayList<Edge>();

	// add all left/right edges
	for (int[] building : buildings) {
		Edge startEdge = new Edge(building[0], building[2], true);
		edges.add(startEdge);
		Edge endEdge = new Edge(building[1], building[2], false);
		edges.add(endEdge);
	}

	// sort edges
	Collections.sort(edges, new Comparator<Edge>() {
		public int compare(Edge a, Edge b) {
			if (a.x != b.x)
				return Integer.compare(a.x, b.x);

			if (a.isStart && b.isStart) {
				return Integer.compare(b.height, a.height);
			}

			if (!a.isStart && !b.isStart) {
				return Integer.compare(a.height, b.height);
			}

			return a.isStart ? -1 : 1;
		}
	});

	// process edges
	PriorityQueue<Integer> heightHeap = new PriorityQueue<Integer>(10, Collections.reverseOrder());

	for (Edge edge : edges) {
		if (edge.isStart) {
			if (heightHeap.isEmpty() || edge.height > heightHeap.peek()) {
				result.add(new int[] { edge.x, edge.height });
			}
			heightHeap.add(edge.height);
		} else {
			heightHeap.remove(edge.height);

			if(heightHeap.isEmpty()){
				result.add(new int[] {edge.x, 0});
			}else if(edge.height > heightHeap.peek()){
				result.add(new int[]{edge.x, heightHeap.peek()});
			}
		}
	}

	return result;
}
时间: 2025-01-20 04:41:40

218. The Skyline Problem的相关文章

[LeetCode#218] The Skyline Problem

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), w

[leedcode 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 pr

Java for LeetCode 218 The Skyline Problem【Comming Soon】

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题218——The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/ 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

[LeetCode-JAVA] The Skyline Problem

题目:题目太长了,见链接-- > The Skyline Problem 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

[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

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

105 - The Skyline Problem(利用判断,在于想法)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=41 解题思路: 当看到题目时,仔细想想感觉用暴力方法挺复杂的.需要判断建筑是否重叠,找到高度的变化位置,找出对应的左值,找出对应的右值.能这么想就很接近我要讲的解题方法了. 我们是需要找到高度转折点,找到最大的右值. 高度转折点的寻找:判断高度是否和前