public int[] findRightInterval(Interval[] intervals) { if (intervals == null) { throw new IllegalArgumentException(); } int len = intervals.length; int[] result = new int[len]; TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(); for (int i = 0; i < len; i++) { map.put(intervals[i].start, i); } for (int i = 0; i < len; i++) { Integer key = map.ceilingKey(intervals[i].end); if (key == null) { result[i] = -1; } else { result[i] = map.get(key); } } return result; }
public List<int[]> getSkyline(int[][] buildings) { List<int[]> result = new ArrayList<>(); if (buildings == null || buildings.length == 0) { return result; } Comparator<Point> comp = new Comparator<Point>() { @Override public int compare(Point a, Point b) { if (a.x != b.x) { return a.x - b.x; } else { return a.y - b.y; } } }; PriorityQueue<Point> pq = new PriorityQueue<>(5, comp); for (int[] data : buildings) { pq.add(new Point(data[0], -data[2])); pq.add(new Point(data[1], data[2])); } TreeMap<Integer, Integer> map = new TreeMap<>(); map.put(0, 1); int max = 0; while (!pq.isEmpty()) { Point cur = pq.poll(); if (cur.y < 0) { if (!map.containsKey(-cur.y)) { map.put(-cur.y, 0); } map.put(-cur.y, map.get(-cur.y) + 1); } else { map.put(cur.y, map.get(cur.y) - 1); if (map.get(cur.y) == 0) { map.remove(cur.y); } } int curMax = map.lastEntry().getKey(); if (curMax != max) { result.add(new int[] {cur.x, curMax}); max = curMax; } } return result; }