< Summary

Information
Class: LeetCode.Algorithms.MyCalendar1.MyCalendar1BinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MyCalendar1\MyCalendar1BinarySearch.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 62
Line coverage: 91.3%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Book(...)80%1010100%
.ctor(...)100%11100%
get_Start()100%11100%
get_End()100%11100%
CompareTo(...)50%2266.66%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MyCalendar1\MyCalendar1BinarySearch.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.MyCalendar1;
 13
 14/// <inheritdoc />
 15public class MyCalendar1BinarySearch : IMyCalendar1
 16{
 117    private readonly List<Item> _items = [];
 18
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="start"></param>
 24    /// <param name="end"></param>
 25    /// <returns></returns>
 26    public bool Book(int start, int end)
 327    {
 328        var item = new Item(start, end);
 29
 330        var index = _items.BinarySearch(item);
 31
 332        if (index < 0)
 333        {
 334            index = ~index;
 335        }
 36
 337        if ((index < _items.Count && _items[index].Start < end) || (index > 0 && _items[index - 1].End > start))
 138        {
 139            return false;
 40        }
 41
 242        _items.Insert(index, item);
 43
 244        return true;
 345    }
 46
 347    private class Item(int start, int end) : IComparable<Item>
 48    {
 749        public int Start { get; } = start;
 550        public int End { get; } = end;
 51
 52        public int CompareTo(Item? other)
 253        {
 254            if (other == null)
 055            {
 056                return -1;
 57            }
 58
 259            return Start.CompareTo(other.Start);
 260        }
 61    }
 62}