| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.TheNumberOfTheSmallestUnoccupiedChair; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public 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) |
| 4 | 25 | | { |
| 4 | 26 | | var freeChairs = new SortedSet<int>(); |
| | 27 | |
|
| 60 | 28 | | for (var i = 0; i < times.Length; i++) |
| 26 | 29 | | { |
| 26 | 30 | | freeChairs.Add(i); |
| 26 | 31 | | } |
| | 32 | |
|
| 4 | 33 | | var chairAssigned = new int[times.Length]; |
| | 34 | |
|
| 4 | 35 | | var events = new List<Event>(); |
| | 36 | |
|
| 60 | 37 | | for (var i = 0; i < times.Length; i++) |
| 26 | 38 | | { |
| 26 | 39 | | events.Add(new Event(times[i][0], i, true)); |
| 26 | 40 | | events.Add(new Event(times[i][1], i, false)); |
| 26 | 41 | | } |
| | 42 | |
|
| 4 | 43 | | events.Sort(); |
| | 44 | |
|
| 80 | 45 | | foreach (var @event in events) |
| 36 | 46 | | { |
| 36 | 47 | | if (@event.IsArrival) |
| 25 | 48 | | { |
| 25 | 49 | | var assignedChair = freeChairs.Min; |
| | 50 | |
|
| 25 | 51 | | freeChairs.Remove(assignedChair); |
| | 52 | |
|
| 25 | 53 | | chairAssigned[@event.FriendId] = assignedChair; |
| | 54 | |
|
| 25 | 55 | | if (@event.FriendId == targetFriend) |
| 4 | 56 | | { |
| 4 | 57 | | return assignedChair; |
| | 58 | | } |
| 21 | 59 | | } |
| | 60 | | else |
| 11 | 61 | | { |
| 11 | 62 | | freeChairs.Add(chairAssigned[@event.FriendId]); |
| 11 | 63 | | } |
| 32 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | return -1; |
| 4 | 67 | | } |
| | 68 | |
|
| 52 | 69 | | private class Event(int time, int friendId, bool isArrival) : IComparable<Event> |
| | 70 | | { |
| 52 | 71 | | private readonly int _time = time; |
| 113 | 72 | | public int FriendId { get; } = friendId; |
| 108 | 73 | | public bool IsArrival { get; } = isArrival; |
| | 74 | |
|
| | 75 | | public int CompareTo(Event? other) |
| 192 | 76 | | { |
| 192 | 77 | | if (other == null) |
| 0 | 78 | | { |
| 0 | 79 | | return -1; |
| | 80 | | } |
| | 81 | |
|
| 192 | 82 | | var timeComparison = _time.CompareTo(other._time); |
| | 83 | |
|
| 192 | 84 | | return timeComparison == 0 ? IsArrival.CompareTo(other.IsArrival) : timeComparison; |
| 192 | 85 | | } |
| | 86 | | } |
| | 87 | | } |