Binary search · Hard
A maintenance worker can run one job at a time. Jobs have fixed windows and different recovery value.
Return the maximum value of a non-overlapping subset of jobs.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ jobs:{start:integer,end:integer,value:integer}[] }. start < end; value may be negative
What each field means
jobs— available jobs, each withstart,endand a signed integervalue.
Output
Return value and rules
- Return the largest total value obtainable from non-overlapping jobs.
- A job may start exactly when another selected job ends.
- Selecting no jobs is allowed and has value
0. Return the value, not the selected jobs.
Constraints
- 0 <= jobs.length <= 100000
- Time bounds are [0,1000000000]; values are [-1000000,1000000]
- Do not mutate or return the selected jobs.
Examples
Example 1
{
"jobs": [
{
"start": 0,
"end": 3,
"value": 5
},
{
"start": 3,
"end": 5,
"value": 6
},
{
"start": 0,
"end": 5,
"value": 10
}
]
}Returns
11Example 2
{
"jobs": []
}Returns
0Required complexity
O(n log n) time and O(n) space