Java Easy to Understand Solution


  • 0
        public int[] findRightInterval(Interval[] intervals) {
            if (intervals == null || intervals.length == 0) return new int[0];
            if (intervals.length == 1) return new int[]{-1};
            Map<Interval, Integer> map = new HashMap<>();
            for (int i = 0; i < intervals.length; i++) map.put(intervals[i], i);
    
            Arrays.sort(intervals, (a, b) -> a.start - b.start);
            int[] res = new int[intervals.length];
            int i = 0;
            while (i < intervals.length) {
                Interval temp = intervals[i];
                int j = i + 1;
                while (j < intervals.length && temp.end > intervals[j].start) j++;
                if (j == intervals.length) res[map.get(temp)] = -1;
                else res[map.get(temp)] = map.get(intervals[j]);
                i++;           
            }
            return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.