DDSA Solutions

1942. The Number of the Smallest Unoccupied Chair

Time: O(n log n)
Space: O(n)

Problem Overview

The Number of the Smallest Unoccupied Chair (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Hash Table pattern in coding interviews. Min-heaps for free chairs and occupied (endTime, chair) pairs; assign smallest available.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

Min-heaps for free chairs and occupied (endTime, chair) pairs; assign smallest available.

Related patterns: Array, Hash Table, Heap (Priority Queue)

1942.cs
C#
// Approach: Min-heaps for free chairs and occupied (endTime, chair) pairs; assign smallest available.
// Time: O(n log n) Space: O(n)

public class Solution
{
    public int SmallestChair(int[][] times, int targetFriend)
    {
        int nextUnsatChair = 0;
        PriorityQueue<int, int> emptyChairs = new PriorityQueue<int, int>();
        PriorityQueue<(int, int), int> occupied = new PriorityQueue<(int, int), int>(Comparer<int>.Create((a, b) => a.CompareTo(b)));

        for (int i = 0; i < times.Length; ++i)
        {
            Array.Resize(ref times[i], times[i].Length + 1);
            times[i][times[i].Length - 1] = i;
        }

        Array.Sort(times, (a, b) => a[0].CompareTo(b[0]));

        foreach (var time in times)
        {
            int arrival = time[0];
            int leaving = time[1];
            int i = time[2];
            while (occupied.Count > 0 && occupied.Peek().Item1 <= arrival)
            {
                int occupiedChair = occupied.Dequeue().Item2;
                emptyChairs.Enqueue(occupiedChair, occupiedChair);
            }
            if (i == targetFriend)
                return emptyChairs.Count == 0 ? nextUnsatChair : emptyChairs.Peek();
            if (emptyChairs.Count == 0)
            {
                occupied.Enqueue((leaving, nextUnsatChair), leaving);
                nextUnsatChair++;
            }
            else
            {
                int emptyChair = emptyChairs.Dequeue();
                occupied.Enqueue((leaving, emptyChair), leaving);
            }
        }

        throw new ArgumentException();
    }
}
Was this solution helpful?

Related Problems