Skip to the code
+25 XP

#398 Fewest Build Agents Needed

medium

A CI system runs build jobs on agents, one job per agent at a time. Job i runs from jobs[i][0] until jobs[i][1] (start < end). An agent that finishes a job at time t can start another job at time t.

Write agentsNeeded(jobs) that returns the smallest number of agents that can run every job at its scheduled time. No jobs need no agents.

Example 1

Input: jobs = [[0,40],[5,15],[20,25]]
Output: 2

The job [0, 40] overlaps both others, but [5, 15] and [20, 25] can share an agent.

Example 2

Input: jobs = [[1,5],[5,9]]
Output: 1

The second job starts exactly when the first ends.

Constraints

  • 0 <= jobs.length <= 2 * 10^5
  • 0 <= start < end <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).