package leetcode;

/**
 * https://leetcode.com/problems/interval-list-intersections/
 */
public class Problem986 {
    public static class Interval {
        int start;
        int end;

        Interval() {
            start = 0;
            end = 0;
        }

        Interval(int s, int e) {
            start = s;
            end = e;
        }

        @Override
        public String toString() {
            return "[" + start + ", " + end + "]";
        }
    }

    public Interval[] intervalIntersection(Interval[] A, Interval[] B) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem986 prob = new Problem986();
        System.out.println(prob.intervalIntersection(
            new Interval[]{
                new Interval(0, 2), new Interval(5, 10), new Interval(13, 23), new Interval(24, 25)
            }, new Interval[]{
                new Interval(1, 5), new Interval(8, 12), new Interval(15, 24), new Interval(25, 26)
            })); // [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

        System.out.println(prob.intervalIntersection(
            new Interval[]{
                new Interval(0, 3), new Interval(4, 6), new Interval(7, 9)
            }, new Interval[]{
                new Interval(1, 8)
            }
        )); // [[1,3],[4,6],[7,8]]
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -398,123 +398,8 @@
    %7D
-%0A%0A        @Override%0A        public String toString() %7B%0A            return %22%5B%22 + start + %22, %22 + end + %22%5D%22;%0A        %7D
 %0A   
@@ -487,783 +487,908 @@
     
-// TODO%0A        return null
+List%3CInterval%3E answer = new ArrayList%3C%3E();%0A        int i = 0
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem986 prob = new Problem986();%0A        System.out.println(prob.intervalIntersection(%0A            new Interval%5B%5D%7B%0A                new Interval(0, 2), new Interval(5, 10), new Interval(13, 23), new Interval(24, 25)%0A            %7D, new Interval%5B%5D%7B%0A                new Interval(1, 5), new Interval(8, 12), new Interval(15, 24), new Interval(25, 26)%0A            %7D)); // %5B%5B1,2%5D,%5B5,5%5D,%5B8,10%5D,%5B15,23%5D,%5B24,24%5D,%5B25,25%5D%5D%0A%0A        System.out.println(prob.intervalIntersection(%0A            new Interval%5B%5D%7B%0A                new Interval(0, 3), new Interval(4, 6), new Interval(7, 9)%0A            %7D, new Interval%5B%5D%7B%0A                new Interval(1, 8)%0A            %7D%0A        )); // %5B%5B1,3%5D,%5B4,6%5D,%5B7,8%5D%5D
+int j = 0;%0A        while (i %3C A.length && j %3C B.length) %7B%0A            if ((A%5Bi%5D.start %3C= B%5Bj%5D.start && B%5Bj%5D.start %3C= A%5Bi%5D.end) %7C%7C%0A                (B%5Bj%5D.start %3C= A%5Bi%5D.start && A%5Bi%5D.start %3C= B%5Bj%5D.end)) %7B%0A                int start = Integer.max(A%5Bi%5D.start, B%5Bj%5D.start);%0A                int end = Integer.min(A%5Bi%5D.end, B%5Bj%5D.end);%0A                if (A%5Bi%5D.end %3C B%5Bj%5D.end) %7B%0A                    i++;%0A                %7D else if (A%5Bi%5D.end == B%5Bj%5D.end) %7B%0A                    i++;%0A                    j++;%0A                %7D else %7B%0A                    j++;%0A                %7D%0A                answer.add(new Interval(start, end));%0A            %7D else if (A%5Bi%5D.start %3C B%5Bj%5D.start) %7B%0A                i++;%0A            %7D else if (B%5Bj%5D.start %3C A%5Bi%5D.start) %7B%0A                j++;%0A            %7D%0A        %7D%0A        return answer.toArray(new Interval%5B0%5D);
 %0A   

Solve problem 986