package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/my-calendar-ii/
 */
public class Problem731 {
    private static class MyCalendarTwo {
        private final List<int[]> list = new ArrayList<>();

        public MyCalendarTwo() {
        }

        public boolean book(int start, int end) {
            int count = 0;
            for (int[] i : list) {
                if (i[0] < end && start < i[1]) {
                    if (count > 1) {
                        return false;
                    }
                    count++;
                }
            }
            if (count > 1) {
                return false;
            }
            list.add(new int[]{start, end});
            return true;
        }
    }

    public static void main(String[] args) {
        MyCalendarTwo calendar = new MyCalendarTwo();
//        System.out.println(calendar.book(1, 10)); // true
//        System.out.println(calendar.book(8, 17)); // true
//        System.out.println(calendar.book(2, 7)); // true

//        System.out.println(calendar.book(1, 10)); // true
//        System.out.println(calendar.book(6, 9)); // true
//        System.out.println(calendar.book(7, 8)); // false

        System.out.println(calendar.book(10, 20)); // true
        System.out.println(calendar.book(50, 60)); // true
        System.out.println(calendar.book(10, 40)); // true
        System.out.println(calendar.book(5, 15)); // false
        System.out.println(calendar.book(20, 30)); // true
        System.out.println(calendar.book(5, 10)); // true
        System.out.println(calendar.book(25, 55)); // true

//        System.out.println(calendar.book(47, 50));
//        System.out.println(calendar.book(1, 10));
//        System.out.println(calendar.book(27, 36));
//        System.out.println(calendar.book(40, 47));
//        System.out.println(calendar.book(20, 27));
//        System.out.println(calendar.book(15, 23));
//        System.out.println(calendar.book(10, 18));
//        System.out.println(calendar.book(27, 36));
//        System.out.println(calendar.book(17, 25));
//        System.out.println(calendar.book(8, 17));
//        System.out.println(calendar.book(24, 33));
//        System.out.println(calendar.book(23, 28));
//        System.out.println(calendar.book(21, 27));
//        System.out.println(calendar.book(47, 50));
//        System.out.println(calendar.book(14, 21));
//        System.out.println(calendar.book(26, 32));
//        System.out.println(calendar.book(16, 21));
//        System.out.println(calendar.book(2, 7)); // true
    }
}

@@ -247,16 +247,78 @@
 ist%3C%3E();
+%0A        private final List%3Cint%5B%5D%3E booked = new ArrayList%3C%3E();
 %0A%0A      
@@ -409,35 +409,8 @@
 ) %7B%0A
-            int count = 0;%0A
     
@@ -432,20 +432,22 @@
 t%5B%5D i : 
-list
+booked
 ) %7B%0A    
@@ -496,49 +496,8 @@
 ) %7B%0A
-                    if (count %3E 1) %7B%0A    
     
@@ -518,36 +518,32 @@
   return false;%0A
-    
                 
@@ -560,2046 +560,293 @@
     
-        count++;%0A                %7D%0A            %7D%0A            if (count %3E 1) %7B%0A                return false;%0A            %7D%0A            list.add(new int%5B%5D%7Bstart, end%7D);%0A            return true;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        MyCalendarTwo calendar = new MyCalendarTwo();%0A//        System.out.println(calendar.book(1, 10)); // true%0A//        System.out.println(calendar.book(8, 17)); // true%0A//        System.out.println(calendar.book(2, 7)); // true%0A%0A//        System.out.println(calendar.book(1, 10)); // true%0A//        System.out.println(calendar.book(6, 9)); // true%0A//        System.out.println(calendar.book(7, 8)); // false%0A%0A        System.out.println(calendar.book(10, 20)); // true%0A        System.out.println(calendar.book(50, 60)); // true%0A        System.out.println(calendar.book(10, 40)); // true%0A        System.out.println(calendar.book(5, 15)); // false%0A        System.out.println(calendar.book(20, 30)); // true%0A        System.out.println(calendar.book(5, 10)); // true%0A        System.out.println(calendar.book(25, 55)); // true%0A%0A//        System.out.println(calendar.book(47, 50));%0A//        System.out.println(calendar.book(1, 10));%0A//        System.out.println(calendar.book(27, 36));%0A//        System.out.println(calendar.book(40, 47));%0A//        System.out.println(calendar.book(20, 27));%0A//        System.out.println(calendar.book(15, 23));%0A//        System.out.println(calendar.book(10, 18));%0A//        System.out.println(calendar.book(27, 36));%0A//        System.out.println(calendar.book(17, 25));%0A//        System.out.println(calendar.book(8, 17));%0A//        System.out.println(calendar.book(24, 33));%0A//        System.out.println(calendar.book(23, 28));%0A//        System.out.println(calendar.book(21, 27));%0A//        System.out.println(calendar.book(47, 50));%0A//        System.out.println(calendar.book(14, 21));%0A//        System.out.println(calendar.book(26, 32));%0A//        System.out.println(calendar.book(16, 21));%0A//        System.out.println(calendar.book(2, 7)); // true
+%7D%0A            for (int%5B%5D i : list) %7B%0A                if (i%5B0%5D %3C end && start %3C i%5B1%5D) %7B%0A                    booked.add(new int%5B%5D%7BMath.max(i%5B0%5D, start), Math.min(end, i%5B1%5D)%7D);%0A                %7D%0A            %7D%0A            list.add(new int%5B%5D%7Bstart, end%7D);%0A            return true;%0A        %7D
 %0A   

Solve problem 731