Advertisement

The Number of the Smallest Unoccupied Chair - LeetCode 1942 Solution

The Number of the Smallest Unoccupied Chair - Complete Solution Guide

The Number of the Smallest Unoccupied Chair is LeetCode problem 1942, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There is a party where n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity . When a friend arrives at the party, they sit on the unoccupied chair with the smallest number . For example, if chairs 0 , 1 , and 5 are occupied when a friend comes, they will sit on chair number 2 . When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment,

Detailed Explanation

The problem simulates a party seating arrangement where friends arrive and leave at different times. The goal is to determine which chair number a specific friend (identified by `targetFriend`) will occupy. Chairs are numbered from 0 to infinity, and arriving friends always take the smallest available chair. When a friend leaves, their chair becomes immediately available for others. We are given an array `times` where each element `times[i] = [arrival_time, leaving_time]` represents the arrival and leaving times of the i-th friend. All arrival times are distinct.

Solution Approach

The solution uses a combination of sorting and priority queues to efficiently simulate the party seating process. First, the friends are sorted by their arrival times. Two priority queues are used: one to store the available chair numbers (`available_chairs`) and another to store the occupied chairs along with their leaving times (`occupied_chairs`). As we iterate through the sorted friends, we first free up any chairs that have become available (leaving time is less than or equal to the current friend's arrival time). Then, we assign the smallest available chair to the current friend. If no chairs are available, we assign the next available chair number. Finally, we store the chair number and the leaving time of the current friend in the `occupied_chairs` queue. The algorithm returns the chair number assigned to the `targetFriend`.

Step-by-Step Algorithm

  1. Step 1: Create a list of tuples `(arrival_time, leaving_time, friend_id)` and sort it based on arrival time.
  2. Step 2: Initialize two min-heaps (priority queues): `available_chairs` (initially empty) and `occupied_chairs` (initially empty). Also initialize `next_chair = 0` to track the next available chair number.
  3. Step 3: Iterate through the sorted list of friends.
  4. Step 4: For each friend, check if any chairs in `occupied_chairs` have become available (leaving time <= current friend's arrival time). If so, move those chairs to `available_chairs`.
  5. Step 5: Assign a chair to the current friend. If `available_chairs` is not empty, assign the smallest chair from it. Otherwise, assign `next_chair` and increment `next_chair`.
  6. Step 6: If the current friend is the `targetFriend`, return the assigned chair number.
  7. Step 7: Add the assigned chair and the friend's leaving time to the `occupied_chairs`.
  8. Step 8: After processing all friends, return the result.

Key Insights

  • Insight 1: Sorting the friends by arrival time is crucial to simulate the real-time arrival process.
  • Insight 2: Maintaining a data structure (like a min-heap or priority queue) to track available and occupied chairs allows efficient retrieval of the smallest available chair and management of chair availability upon departure.
  • Insight 3: We need to keep track of currently occupied chairs with their leaving times so we can free them up at the appropriate arrival times of other guests.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Heap (Priority Queue).

Companies

Asked at: Otter.ai.