All problems
1172HardArrayBinary SearchDynamic ProgrammingSorting

Taking at Most k Bookings

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1751Maximum Number of Events That Can Be Attended II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A hall has bookings on offer. bookings[i] = [first, last, fee] runs from day first to day last, both days included, and pays fee.

Take at most k of them, and no two taken bookings may share a day.

Return the largest total fee that can be earned.

Examples

Example 1

Input
bookings = [[1, 2, 4], [2, 3, 10]], k = 2
Output
10

The two bookings share day 2, so only one may be taken and the larger fee wins.

Example 2

Input
bookings = [[1, 10, 5], [2, 3, 4], [5, 6, 4]], k = 2
Output
8

The long booking pays 5 on its own, while the two short ones share no day and pay 4 each.

Example 3

Input
bookings = [[1, 2, 1], [3, 4, 1], [5, 6, 1], [7, 8, 1]], k = 2
Output
2

The four bookings share no days at all, but only two of them may be taken.

Constraints

  • 1 <= k <= bookings.length
  • 1 <= k * bookings.length <= 10^6
  • bookings[i].length == 3
  • 1 <= bookings[i][0] <= bookings[i][1] <= 10^9
  • 1 <= bookings[i][2] <= 10^6

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def max_value(bookings: list[list[int]], k: int) -> int:
Java
public int maxValue(int[][] bookings, int k)
September 7
Apply