< Summary

Information
Class: LeetCode.Algorithms.TheNumberOfTheSmallestUnoccupiedChair.TheNumberOfTheSmallestUnoccupiedChairPriorityQueue
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TheNumberOfTheSmallestUnoccupiedChair\TheNumberOfTheSmallestUnoccupiedChairPriorityQueue.cs
Line coverage
95%
Covered lines: 22
Uncovered lines: 1
Coverable lines: 23
Total lines: 59
Line coverage: 95.6%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SmallestChair(...)90%101095.65%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TheNumberOfTheSmallestUnoccupiedChair\TheNumberOfTheSmallestUnoccupiedChairPriorityQueue.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 TheNumberOfTheSmallestUnoccupiedChairPriorityQueue : 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 targetFriendArrival = times[targetFriend][0];
 27
 7028        Array.Sort(times, (a, b) => a[0] - b[0]);
 29
 430        var availablePriorityQueue = new PriorityQueue<int, int>();
 431        var occupiedPriorityQueue = new PriorityQueue<(int Chair, int Leaving), int>();
 32
 433        var currentChair = 0;
 34
 5835        foreach (var time in times)
 2536        {
 2537            var arrival = time[0];
 2538            var leaving = time[1];
 39
 3640            while (occupiedPriorityQueue.Count > 0 && occupiedPriorityQueue.Peek().Leaving <= arrival)
 1141            {
 1142                var availableChair = occupiedPriorityQueue.Dequeue().Chair;
 43
 1144                availablePriorityQueue.Enqueue(availableChair, availableChair);
 1145            }
 46
 2547            var nextChair = availablePriorityQueue.Count == 0 ? currentChair++ : availablePriorityQueue.Dequeue();
 48
 2549            if (arrival == targetFriendArrival)
 450            {
 451                return nextChair;
 52            }
 53
 2154            occupiedPriorityQueue.Enqueue((nextChair, leaving), leaving);
 2155        }
 56
 057        return -1;
 458    }
 59}