Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
963 B
2
Indexable
Never
public class MyCalendar
{
    List<(int, int)> bookings;
    BookingComparer bookingComparer;
    
    public MyCalendar()
    {
        bookingComparer = new BookingComparer();
        bookings = new();
    }
    
    public bool Book(int start, int end)
    {
        var booking = (start, end);
        
        var index = bookings.BinarySearch((start, end), bookingComparer);
        
        if (index < 0)
        {
            bookings.Insert(~index, booking);
            return true;
        }
        return false;
    }
}


public class BookingComparer : IComparer<(int, int)>
{
    // (int, int) -> (start, end) -> (Item1, Item2)
    public int Compare((int, int) first, (int, int) second)
    {
        if (first.Item2 <= second.Item1)
        {
            return -1;
        }
        
        if (first.Item1 >= second.Item2)
        {
            return 1;
        }
        
        return 0;
    }
}