NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

Given a list of jobs stored by start time. Each job consists of three parts:

  • Job ID: A unique identifier (e.g., "J1").
  • Start Time: A 24-hour time in "HHMM" format (e.g., "0023" represents 00:23).
  • Duration: The time in minutes required to complete the job.

Your task is to assign each job to a machine following these rules:

  • A machine can work on only one job at a time.
  • A machine becomes available once its current job's finish time is less than or equal to the start time of a new job.
  • When multiple machines are free, assign the job to the machine with the smallest index.
  • If no machine is available at the new job's start time, allocate a new machine with the maximum index + 1.

Return a list where each element represents a job and its assigned machine (machine numbers are prefixed by "M") in the format ["job_id", "machine_id"].

Constraints:

  • Jobs are sorted by the start time when given.
  • Jobs are processed in the order provided.
  • Start times are given in "HHMM" format.
  • Duration is provided in minutes.
  • All jobs are scheduled for the same day.

Example 1:

Input: jobs = [["J1", "0023", "45"], ["J2", "0025", "10"], ["J3", "0100", "60"], ["J4", "0300", "10"]] Output: [["J1", "M1"], ["J2", "M2"], ["J3", "M2"], ["J4", "M1"]] Explanation:

  • J1: starts at 00:23. Machine M1 is allocated, and J1 will finish at 00:23 + 45 min = 00:68.
  • J2: starts at 00:25. Since M1 is busy until 00:68, a new machine M2 is allocated. J2 finishes at 00:25 + 10 min = 00:35.
  • J3: starts at 01:00. At this time, M2 (which finished at 00:35) is free, so J3 is assigned to M2.
  • J4: starts at 03:00. At this time, M1 (which finished at 01:08) and M2 (which finished at 02:00) are both free, so J4 is assigned to M1 due to the smaller index.

Example 2:

Input: jobs = [["J1", "0000", "30"], ["J2", "0030", "15"], ["J3", "0045", "20"]] Output: [["J1", "M1"], ["J2", "M1"], ["J3", "M1"]]

Example 3:

Input: jobs = [["J1", "0100", "50"], ["J2", "0105", "30"], ["J3", "0110", "20"], ["J4", "0120", "30"]] Output: [["J1", "M1"], ["J2", "M2"], ["J3", "M3"], ["J4", "M4"]]


Accepted
1.2k/2.6k
Acceptance Rate46.3%
Test Cases
Input
jobs =
Output
Expected