Ctrl K

The Best-Paying Gigs

Hard
A musician is offered gigs: gigs[i] = [start, end, pay] means the gig takes days start through end, inclusive, and pays pay. Only one gig can be played at a time, and a gig that is taken must be played in full. The end day counts, so two gigs where one ends on the day the other begins cannot both be taken. At most limit gigs may be taken (fewer is fine). Return the largest total pay.
Example 1
gigs=[[1, 3, 5], [4, 6, 3], [2, 5, 7], [7, 7, 2]], limit=2 → 9

The best choice is the gigs [2, 5] and [7, 7]: 7 + 2 = 9. The pair [1, 3] and [4, 6] only gives 8.

Example 2
gigs=[[1, 3, 5], [4, 6, 3], [2, 5, 7], [7, 7, 2]], limit=3 → 10

Now three fit: [1, 3], [4, 6] and [7, 7], for 5 + 3 + 2 = 10.

Example 3
gigs=[[1, 2, 3], [2, 4, 4]], limit=2 → 4

Both gigs use day 2, so only one can be taken, and the better-paid one wins.

Constraints
  • 1 ≤ limit ≤ len(gigs)
  • 1 ≤ limit · len(gigs) ≤ 10⁶
  • 1 ≤ start ≤ end ≤ 10⁹
  • 1 ≤ pay ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardStrict Climb From Spare Parts HardBest-Paying Set of Gigs HardClimbing Along Rows and Columns HardWords Hidden in a Text Medium
loading Python…
Input
gigs=[[1, 3, 5], [4, 6, 3], [2, 5, 7], [7, 7, 2]], limit=2
Expected
9

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.