Problem
Ask
Submissions

Problem: The Number of the Smallest Unoccupied Chair

Medium
30 min
Explore how to apply heap data structures to solve dynamic allocation problems by tracking the smallest unoccupied chair for arriving friends. This lesson helps you understand the use of heaps for real-time data processing and optimization, enabling you to implement effective coding solutions in interviews.

Statement

At a party, nn friends, numbered from 00 to n1n - 1, arrive and leave at different times. There are infinitely many chairs, numbered 00 onwards. Each arriving friend sits on the smallest available chair at that moment.

For example, if chairs 00, 11, and 55 are occupied when a friend arrives, they will sit on chair number 22.

When a friend leaves, their chair becomes immediately available. If another friend arrives simultaneously, they can take that chair.

You are given a 00-indexed 2D2D list times, where times[i] =[arrivali,leavingi]= [ arrival_i, leaving_i] represents the arrival and departure times of the ithi_{th} friend. All arrival times are unique.

Given an integer target_friend, return the chair number that target_friend will sit on.

Constraints:

  • n==n == times.length

  • times[i].length ==2== 2

  • 11\leq arrivali < leavingi 105\leq 10^5

  • 00 \leq target_friend n1\leq n - 1

  • Each arrivali time is unique.

Problem
Ask
Submissions

Problem: The Number of the Smallest Unoccupied Chair

Medium
30 min
Explore how to apply heap data structures to solve dynamic allocation problems by tracking the smallest unoccupied chair for arriving friends. This lesson helps you understand the use of heaps for real-time data processing and optimization, enabling you to implement effective coding solutions in interviews.

Statement

At a party, nn friends, numbered from 00 to n1n - 1, arrive and leave at different times. There are infinitely many chairs, numbered 00 onwards. Each arriving friend sits on the smallest available chair at that moment.

For example, if chairs 00, 11, and 55 are occupied when a friend arrives, they will sit on chair number 22.

When a friend leaves, their chair becomes immediately available. If another friend arrives simultaneously, they can take that chair.

You are given a 00-indexed 2D2D list times, where times[i] =[arrivali,leavingi]= [ arrival_i, leaving_i] represents the arrival and departure times of the ithi_{th} friend. All arrival times are unique.

Given an integer target_friend, return the chair number that target_friend will sit on.

Constraints:

  • n==n == times.length

  • times[i].length ==2== 2

  • 11\leq arrivali < leavingi 105\leq 10^5

  • 00 \leq target_friend n1\leq n - 1

  • Each arrivali time is unique.