java 218.天际线问题(#Heap).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 218.天际线问题(#Heap).java相关的知识,希望对你有一定的参考价值。


/*
 Detailed explanation: http://www.geeksforgeeks.org/divide-and-conquer-set-7-the-skyline-problem/
*/
public class Solution {
	public List<int[]> getSkyline(int[][] buildings) {
		if (buildings.length == 0)
			return new LinkedList<int[]>();
		return recurSkyline(buildings, 0, buildings.length - 1);
	}

	private LinkedList<int[]> recurSkyline(int[][] buildings, int p, int q) {
		if (p < q) {
			int mid = p + (q - p) / 2;
			return merge(recurSkyline(buildings, p, mid),
					recurSkyline(buildings, mid + 1, q));
		} else {
			LinkedList<int[]> rs = new LinkedList<int[]>();
			rs.add(new int[] { buildings[p][0], buildings[p][2] });
			rs.add(new int[] { buildings[p][1], 0 });
			return rs;
		}
	}

	private LinkedList<int[]> merge(LinkedList<int[]> l1, LinkedList<int[]> l2) {
		LinkedList<int[]> rs = new LinkedList<int[]>();
		int h1 = 0, h2 = 0;
		while (l1.size() > 0 && l2.size() > 0) {
			int x = 0, h = 0;
			if (l1.getFirst()[0] < l2.getFirst()[0]) {
				x = l1.getFirst()[0];
				h1 = l1.getFirst()[1];
				h = Math.max(h1, h2);
				l1.removeFirst();
			} else if (l1.getFirst()[0] > l2.getFirst()[0]) {
				x = l2.getFirst()[0];
				h2 = l2.getFirst()[1];
				h = Math.max(h1, h2);
				l2.removeFirst();
			} else {
				x = l1.getFirst()[0];
				h1 = l1.getFirst()[1];
				h2 = l2.getFirst()[1];
				h = Math.max(h1, h2);
				l1.removeFirst();
				l2.removeFirst();
			}
			if (rs.size() == 0 || h != rs.getLast()[1]) {
				rs.add(new int[] { x, h });
			}
		}
		rs.addAll(l1);
		rs.addAll(l2);
		return rs;
	}
}
public class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> res = new ArrayList<>();
        List<int[]> points = new ArrayList<>();
        if (buildings == null || buildings.length < 1) return res;
        for (int[] b : buildings) {
            points.add(new int[] {b[0], -1 * b[2]}); 
            /* designed for edge cases : 
            1. buildings that have the same start position but different end positions
            2. buildings that have the same end position
            3. buildings that overlap between the starting point and the end point
            */
                                                                                  
            points.add(new int[] {b[1], b[2]});
        }
        Collections.sort(points, new Comparator<int[]>(){
            public int compare(int[] a, int[] b) {
                if (a[0] == b[0]) {
                    return a[1] - b[1];
                }
                return a[0] - b[0];
            }
        });
        
        PriorityQueue<Integer> maxHeight = new PriorityQueue<>(new Comparator<Integer>(){
            public int compare(Integer a, Integer b) {
                return b - a;
            }
        });
        
        maxHeight.offer(0);
        int pre = 0;
        for (int[] p : points) {
            if (p[1] < 0) {
                maxHeight.offer(-1 * p[1]);
            } else {
                maxHeight.remove(p[1]);
            }
            
            int cur = maxHeight.peek();
            if (pre != cur) {
                res.add(new int[] {p[0], cur});
                pre = cur;
            }
        }
        return res;
    }
}

以上是关于java 218.天际线问题(#Heap).java的主要内容,如果未能解决你的问题,请参考以下文章

java 218.天际线问题(#Heap).java

java 218.天际线问题(#Heap).java

java 218.天际线问题(#Heap).java

java 218.天际线问题(#Heap).java

题目地址(218. 天际线问题)

题目地址(218. 天际线问题)