Sweep line · Medium
An async job service knows the half-open execution intervals of accepted jobs. It needs enough workers for the busiest instant.
Return the maximum number of simultaneously active jobs.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ jobs:[start:integer,end:integer][] }
Input rules
- Every job has start < end.
What each field means
jobs—[start, end]intervals. A job is active at its start, but not at its end.
Output
Return value and rules
- Return the largest number of jobs active at the same time.
- A job ending at time
tdoes not overlap one starting att. - No jobs require zero workers.
Constraints
- 0 <= jobs.length <= 100000
- 0 <= start < end <= 1000000000
Examples
Example 1
{
"jobs": [
[
0,
4
],
[
1,
3
],
[
3,
5
]
]
}Returns
2Example 2
{
"jobs": []
}Returns
0Required complexity
O(n log n) time and O(n) space