1. 程式人生 > >LeetCode: 436. Find Right Interval

LeetCode: 436. Find Right Interval

public class Solution {
    public int[] findRightInterval(Interval[] intervals) {
    	NavigableMap<Integer, Integer> map = new TreeMap<>();
    	int[] result = new int[intervals.length];
    	for(int i = 0; i < intervals.length; i++) {
    		map.put(intervals[i].start, i);
    	}
    	for (int i = 0; i < intervals.length; ++i) {
            Map.Entry<Integer, Integer> entry = map.ceilingEntry(intervals[i].end);
            result[i] = (entry != null) ? entry.getValue() : -1;
        }
		return result;
    }
}