Gift delivery scheduler with limited agents
I was asked this
No reports yet. Sign in to say where you were asked it.
You are planning gift deliveries to employees in an office. The workday runs from hour 9 to hour 17.
You get:
-
intervals, a list of[start, end]pairs with integer hours. Employeeiis in the office fromstart_iuntilend_i. -
n, the number of delivery agents.
Rules:
- A delivery takes exactly one hour and must start on a whole hour.
- A delivery to employee
ithat starts at hourhis allowed only ifstart_i <= handh + 1 <= end_i. - Each agent makes at most one delivery per hour, so at most
ndeliveries can happen in the same hour. - Each employee gets at most one gift.
Implement schedule_deliveries(intervals, n). Return the largest number of employees who can get their gift, and a schedule that achieves it: for each employee, the start hour of their delivery, or None if they are not served.
Constraints: 0 <= len(intervals) <= 10^5, 9 <= start <= end <= 17, 0 <= n <= 10^5. Aim for better than trying all assignments.
Example 1
intervals = [[9, 11], [9, 10], [9, 10]], n = 1
-> 2
Only one agent works. One of the two employees who leave at 10 gets the 9:00 slot, and the employee who stays until 11 gets the 10:00 slot. The other early leaver misses out.
Example 2
intervals = [[9, 12], [10, 11], [9, 10]], n = 1
-> 3 schedule: [11, 10, 9]
Serve whoever leaves first: [9, 10] at 9:00, [10, 11] at 10:00, and [9, 12] at 11:00.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
