Non-Overlapping Shows for Maximum Profit
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
You rent out one hall. shows[i] = [s, e, p] is a show that starts at time s, ends at time e (s < e) and pays
p. Only one show can run at a time; a show may start exactly when the previous one ends (e ≤ s' means no overlap).
Return the maximum total profit of a set of pairwise non-overlapping shows.
Examples
| shows | result |
|---|---|
[[1, 3, 50], [3, 5, 20], [2, 6, 100], [6, 7, 10]] | 110 |
[[0, 10, 5], [0, 5, 3], [5, 10, 3]] | 6 |
[[1, 2, 7]] | 7 |
Example 1 — [2, 6) (100) plus [6, 7) (10).
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(shows: int[][]) → long
Constraints
-
shows.length -
se,p - The answer may exceed 32 bits.
Expected complexity
- Time
- O(N log N)
- Space
- O(N)
Examples
| shows | Expected |
|---|---|
[[1,3,50],[3,5,20],[2,6,100],[6,7,10]] | 110 |
[[0,10,5],[0,5,3],[5,10,3]] | 6 |
[[1,2,7]] | 7 |
Hint
Hint
Greedy by end time fails with profits. Sort by end time; best[i] = max(best[i-1], p_i + best[j]) where j is the number
of shows ending at or before show i starts — found by binary search over the sorted end times. Scanning all earlier shows
for every show is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 270 ms | estimated |
| C++ (GCC, gnu++23) | 270 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 2250 ms | derived |
| Kotlin 2.4 (JVM) | 1540 ms | estimated |
| C# 14 (.NET 10) | 1540 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 11 | verdict only |
| performance | 60 | 4 | verdict only |