Solving Leetcode Interviews in Seconds with AI: Exam Room
Introduction
In this blog post, we will explore how to solve the LeetCode problem "855" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
There is an exam room with n seats in a single row labeled from 0 to n - 1. When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number 0. Design a class that simulates the mentioned exam room. Implement the ExamRoom class: ExamRoom(int n) Initializes the object of the exam room with the number of the seats n. int seat() Returns the label of the seat at which the next student will set. void leave(int p) Indicates that the student sitting at seat p will leave the room. It is guaranteed that there will be a student sitting at seat p. Example 1: Input ["ExamRoom", "seat", "seat", "seat", "seat", "leave", "seat"] [[10], [], [], [], [], [4], []] Output [null, 0, 9, 4, 2, null, 5] Explanation ExamRoom examRoom = new ExamRoom(10); examRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0. examRoom.seat(); // return 9, the student sits at the last seat number 9. examRoom.seat(); // return 4, the student sits at the last seat number 4. examRoom.seat(); // return 2, the student sits at the last seat number 2. examRoom.leave(4); examRoom.seat(); // return 5, the student sits at the last seat number 5. Constraints: 1 <= n <= 109 It is guaranteed that there is a student sitting at seat p. At most 104 calls will be made to seat and leave.
Explanation
- Maintain Sorted Seats: Use a sorted set (e.g.,
SortedSetin Python or equivalent) to keep track of the occupied seats. This allows efficient finding of adjacent occupied seats.- Calculate Max Distance: For each
seat()call, iterate through the sorted seats to calculate the maximum distance to the nearest occupied seat for each available position. Handle edge cases (seat 0 and seat n-1) separately. - Efficient Seat Removal:
leave()operation will efficiently remove student id from the sorted set.
- Calculate Max Distance: For each
- Runtime Complexity:
seat(): O(k),leave(): O(log k), where k is the number of occupied seats. Storage Complexity: O(k), where k is the number of occupied seats.
Code
from sortedcontainers import SortedSet
class ExamRoom:
def __init__(self, n: int):
self.n = n
self.seats = SortedSet()
def seat(self) -> int:
if not self.seats:
seat = 0
else:
max_dist = self.seats[0]
seat = 0
for i in range(1, len(self.seats)):
dist = (self.seats[i] - self.seats[i - 1]) // 2
if dist > max_dist:
max_dist = dist
seat = self.seats[i - 1] + dist
if self.n - 1 - self.seats[-1] > max_dist:
seat = self.n - 1
self.seats.add(seat)
return seat
def leave(self, p: int) -> None:
self.seats.remove(p)