package com.fishercoder.solutions;

import com.fishercoder.common.classes.Interval;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class _436 {

    public static class Solution1 {
        public int[] findRightInterval(Interval[] intervals) {
            if (intervals == null || intervals.length == 0) {
                return new int[0];
            }
            int[] result = new int[intervals.length];
            result[0] = -1;
            Interval last = intervals[intervals.length - 1];
            Interval first = intervals[0];
            Map<Interval, Integer> map = new HashMap();
            for (int i = 0; i < intervals.length; i++) {
                map.put(intervals[i], i);
            }

            Collections.sort(Arrays.asList(intervals), (o1, o2) -> o1.start - o2.start);

            for (int i = 1; i < intervals.length; i++) {
                //TODO: use binary search for the minimum start interval for interval[i-1] instead of a while loop
                int tmp = i - 1;
                int tmpI = i;
                while (tmpI < intervals.length && intervals[tmpI].start < intervals[tmp].end) {
                    tmpI++;
                }
                if (tmpI < intervals.length) {
                    result[map.get(intervals[tmp])] = map.get(intervals[tmpI]);
                } else {
                    result[map.get(intervals[tmp])] = -1;
                }
            }
            if (result[intervals.length - 1] == 0 && last.end > first.start) {
                result[intervals.length - 1] = -1;
            }
            return result;
        }
    }

}

@@ -40,138 +40,8 @@
 ort 
-com.fishercoder.common.classes.Interval;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport 
 java
@@ -46,16 +46,20 @@
 va.util.
+Tree
 Map;%0A%0Apu
@@ -152,24 +152,21 @@
 nterval(
-Interval
+int%5B%5D
 %5B%5D inter
@@ -189,106 +189,56 @@
     
-if (intervals == null %7C%7C intervals.length == 0) %7B%0A                return new int%5B0%5D;%0A            %7D
+TreeMap%3CInteger, Integer%3E map = new TreeMap%3C%3E();
 %0A   
@@ -255,19 +255,16 @@
 nt%5B%5D res
-ult
  = new i
@@ -289,196 +289,8 @@
 h%5D;%0A
-            result%5B0%5D = -1;%0A            Interval last = intervals%5Bintervals.length - 1%5D;%0A            Interval first = intervals%5B0%5D;%0A            Map%3CInterval, Integer%3E map = new HashMap();%0A
     
@@ -378,16 +378,19 @@
 rvals%5Bi%5D
+%5B0%5D
 , i);%0A  
@@ -405,99 +405,8 @@
   %7D%0A
-%0A            Collections.sort(Arrays.asList(intervals), (o1, o2) -%3E o1.start - o2.start);%0A%0A
     
@@ -426,17 +426,17 @@
 int i = 
-1
+0
 ; i %3C in
@@ -478,679 +478,130 @@
     
-//TODO: use binary search for the minimum start interval for interval%5Bi-1%5D instead of a while loop%0A                int tmp = i - 1;%0A                int tmpI = i;%0A                while (tmpI %3C intervals.length && intervals%5BtmpI%5D.start %3C intervals%5Btmp%5D.end) %7B%0A                    tmpI++;%0A                %7D%0A                if (tmpI %3C intervals.length) %7B%0A                    result%5Bmap.get(intervals%5Btmp%5D)%5D = map.get(intervals%5BtmpI%5D);%0A                %7D else %7B%0A                    result%5Bmap.get(intervals%5Btmp%5D)%5D = -1;%0A                %7D%0A            %7D%0A            if (result%5Bintervals.length - 1%5D == 0 && last.end %3E first.start) %7B%0A                result%5Bintervals.length - 1%5D =
+Integer key = map.ceilingKey(intervals%5Bi%5D%5Bintervals%5Bi%5D.length - 1%5D);%0A                res%5Bi%5D = key != null ? map.get(key) :
  -1;
@@ -641,11 +641,8 @@
  res
-ult
 ;%0A  

refactor 436