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, they can sit in that chair.
You are given a 0-indexed 2D integer array times
where times[i] = [arrival<sub>i</sub>, leaving<sub>i</sub>]
, indicating the arrival and leaving times of the i<sup>th</sup>
friend respectively, and an integer targetFriend
. All arrival times are distinct.
Return the chair number that the friend numbered targetFriend
will sit on.
Example 1:
Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1
Output: 1
Explanation:
- Friend 0 arrives at time 1 and sits on chair 0.
- Friend 1 arrives at time 2 and sits on chair 1.
- Friend 1 leaves at time 3 and chair 1 becomes empty.
- Friend 0 leaves at time 4 and chair 0 becomes empty.
- Friend 2 arrives at time 4 and sits on chair 0.
Since friend 1 sat on chair 1, we return 1.
Example 2:
Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
Output: 2
Explanation:
- Friend 1 arrives at time 1 and sits on chair 0.
- Friend 2 arrives at time 2 and sits on chair 1.
- Friend 0 arrives at time 3 and sits on chair 2.
- Friend 1 leaves at time 5 and chair 0 becomes empty.
- Friend 2 leaves at time 6 and chair 1 becomes empty.
- Friend 0 leaves at time 10 and chair 2 becomes empty.
Since friend 0 sat on chair 2, we return 2.
What is the chair number that the targetFriend
will sit on?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the smallest available chair number for a friend. We'll try assigning chairs one by one, starting from the smallest, until we find an empty one for our friend. We'll need to keep track of which chairs are taken.
Here's how the algorithm would work step-by-step:
def find_smallest_unoccupied_chair(occupied_chairs):
chair_number = 0
while True:
# Check if the current chair is occupied.
if chair_number not in occupied_chairs:
# If the chair is not occupied, return the chair number.
return chair_number
# Move to the next chair number.
chair_number += 1
#The following is an example of how the function would be invoked.
#occupied_chairs = [0,1,2,3,5]
#smallest_available_chair = find_smallest_unoccupied_chair(occupied_chairs)
#print(smallest_available_chair)
The key to solving this problem efficiently is to keep track of which chairs are available and quickly find the smallest available one. We can accomplish this using a data structure that helps us efficiently manage available and occupied chairs.
Here's how the algorithm would work step-by-step:
import heapq
def smallest_unoccupied_chair(
arrival_times: list[list[int]], target_friend: int) -> int:
events = []
for index, arrival_time in enumerate(arrival_times):
events.append((arrival_time[0], 'arrival', index))
events.append((arrival_time[1], 'departure', index))
events.sort()
available_chairs = []
heapq.heappush(available_chairs, 0)
assigned_chairs = {}
for time, event_type, person_index in events:
if event_type == 'arrival':
# Assign the smallest available chair.
chair_number = heapq.heappop(available_chairs)
assigned_chairs[person_index] = chair_number
if person_index == target_friend:
return chair_number
# Ensure that there is always a next smallest available chair.
if not available_chairs:
heapq.heappush(available_chairs,
chair_number + 1)
else:
# Free up the chair when someone leaves.
chair_to_release = assigned_chairs[person_index]
heapq.heappush(available_chairs, chair_to_release)
Case | How to Handle |
---|---|
Empty arrivals and departures arrays | Return 0 as no seats are occupied yet, and the target friend needs chair 0. |
Target friend arrives and departs at the same time as another friend | The solution should handle ties consistently (e.g., by arrival time) to determine seat assignment. |
Arrival and departure times are very close together | Ensure the algorithm correctly handles cases where seats are freed up almost immediately after being occupied. |
Large number of friend arrivals and departures | Consider the scalability of the solution; using a priority queue or set is crucial for performance. |
Target friend arrives first | The target friend should be assigned chair 0 if no one is occupying chairs before their arrival. |
Target friend arrives last, and all chairs before k are occupied | The algorithm should correctly find the smallest available chair, which will be k or higher. |
Arrival and departure times are the same value for multiple friends | The solution should consistently handle the order of processing these simultaneous events. |
Integer overflow potential with large arrival and departure times | Use appropriate data types (e.g., long) to prevent potential integer overflows in time calculations. |