< Summary

Information
Class: LeetCode.Algorithms.TheNumberOfTheSmallestUnoccupiedChair.TheNumberOfTheSmallestUnoccupiedChairSortedSet
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TheNumberOfTheSmallestUnoccupiedChair\TheNumberOfTheSmallestUnoccupiedChairSortedSet.cs
Line coverage
92%
Covered lines: 39
Uncovered lines: 3
Coverable lines: 42
Total lines: 87
Line coverage: 92.8%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SmallestChair(...)90%101096.77%
.ctor(...)100%11100%
get_FriendId()100%11100%
get_IsArrival()100%11100%
CompareTo(...)75%4471.42%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TheNumberOfTheSmallestUnoccupiedChair\TheNumberOfTheSmallestUnoccupiedChairSortedSet.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.TheNumberOfTheSmallestUnoccupiedChair;
 13
 14/// <inheritdoc />
 15public class TheNumberOfTheSmallestUnoccupiedChairSortedSet : ITheNumberOfTheSmallestUnoccupiedChair
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="times"></param>
 22    /// <param name="targetFriend"></param>
 23    /// <returns></returns>
 24    public int SmallestChair(int[][] times, int targetFriend)
 425    {
 426        var freeChairs = new SortedSet<int>();
 27
 6028        for (var i = 0; i < times.Length; i++)
 2629        {
 2630            freeChairs.Add(i);
 2631        }
 32
 433        var chairAssigned = new int[times.Length];
 34
 435        var events = new List<Event>();
 36
 6037        for (var i = 0; i < times.Length; i++)
 2638        {
 2639            events.Add(new Event(times[i][0], i, true));
 2640            events.Add(new Event(times[i][1], i, false));
 2641        }
 42
 443        events.Sort();
 44
 8045        foreach (var @event in events)
 3646        {
 3647            if (@event.IsArrival)
 2548            {
 2549                var assignedChair = freeChairs.Min;
 50
 2551                freeChairs.Remove(assignedChair);
 52
 2553                chairAssigned[@event.FriendId] = assignedChair;
 54
 2555                if (@event.FriendId == targetFriend)
 456                {
 457                    return assignedChair;
 58                }
 2159            }
 60            else
 1161            {
 1162                freeChairs.Add(chairAssigned[@event.FriendId]);
 1163            }
 3264        }
 65
 066        return -1;
 467    }
 68
 5269    private class Event(int time, int friendId, bool isArrival) : IComparable<Event>
 70    {
 5271        private readonly int _time = time;
 11372        public int FriendId { get; } = friendId;
 10873        public bool IsArrival { get; } = isArrival;
 74
 75        public int CompareTo(Event? other)
 19276        {
 19277            if (other == null)
 078            {
 079                return -1;
 80            }
 81
 19282            var timeComparison = _time.CompareTo(other._time);
 83
 19284            return timeComparison == 0 ? IsArrival.CompareTo(other.IsArrival) : timeComparison;
 19285        }
 86    }
 87}