JAVA Solution Beat 85%


  • 0
    J
    public class Solution {
        public int[] findRightInterval(Interval[] intervals) {
            if (intervals == null || intervals.length == 0) return new int[0];
            int len = intervals.length, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
            int res[] = new int[len];
            HashMap<Integer, Integer> map = new HashMap<>();
            
            for (int i = 0; i < len; i++) {
                if (intervals[i].start > max) max = intervals[i].start;
                if (intervals[i].start < min) min = intervals[i].start;
                map.put(intervals[i].start, i);
            }
            
            for (int i = 0; i < len; i++) {
                int tem = intervals[i].end;
                while (tem <= max && map.get(tem) == null) {
                    tem++;
                }
                if (tem > max) res[i] = -1;
                else if (map.get(tem) != null) {
                    res[i] = map.get(tem);
                }
            }
            
            return res;
        }
    }

Log in to reply
 

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