Dynamic programming · Medium
A single resource receives booking requests with a start time, end time and value. Requests may overlap, and selecting the earliest finish or highest individual value does not necessarily maximize the total. The service may decline every request if none improves the result.
Return the maximum sum of values for a set of pairwise non-overlapping half-open bookings without forcing selection of a negative-value request.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ intervals: [integer, integer, integer][] }
Fields
- Each [start,end,value] has start < end and occupies [start,end). A booking ending at t is compatible with one starting at t.
- Requests may repeat, but overlapping duplicates cannot both be accepted. Values may be negative, zero or positive.
Output
Return value
- Return one integer total value. Empty input or exclusively negative requests returns zero.
- The output is not the number of bookings and does not include the chosen schedule.
Constraints
- 0 <= intervals.length <= 100000; endpoints and values are integers from -1000000 to 1000000.
- All intervals have positive duration. Do not mutate input. Total values fit safe JSON integers.
Examples
Example 1
{
"intervals": [
[
0,
4,
7
],
[
0,
2,
4
],
[
2,
4,
4
]
]
}Returns
8Example 2
{
"intervals": [
[
0,
1,
-2
],
[
1,
2,
0
]
]
}Returns
0Required complexity
O(n log(n + 1)) time and O(n) auxiliary space