zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    PCL配置即常见问题
    opencv2.4.9配置+VS2013
    我的项目配置问题及解决
    Java 8 函数式编程
    leecode刷题(17)-- 实现StrStr
    leecode刷题(16)-- 字符串转换整数
    leecode刷题(15)-- 验证回文字符串
    博客迁移通知
    leecode刷题(14)-- 有效的字母异位词
    leecode刷题(13) -- 字符串中的第一个唯一字符
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5874449.html
Copyright © 2011-2022 走看看