Ctrl K

Two Festival Tickets

Medium
A festival runs a number of shows. Show shows[i] = [start, end, value] runs from day start through day end inclusive, and you rate it value. You may attend at most two shows, and they must not share a single day: if one show ends on day t, the other may start on day t + 1 at the earliest. Return the largest total rating you can collect.
Example 1
shows=[[2, 4, 5], [5, 9, 3], [3, 7, 7]] → 8

Shows 0 and 1 do not overlap: the first ends on day 4 and the second starts on day 5. Together they give 5 + 3 = 8, more than show 2 alone at 7.

Example 2
shows=[[1, 6, 4], [6, 8, 4], [2, 9, 10]] → 10

The first two shows share day 6, so you cannot attend both. The best plan is show 2 alone, worth 10.

Example 3
shows=[[10, 10, 1], [1, 3, 2], [4, 9, 6]] → 8
Constraints
  • 2 ≤ len(shows) ≤ 10⁵
  • shows[i] = [start, end, value]
  • 1 ≤ start ≤ end ≤ 10⁹
  • 1 ≤ value ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Smallest in a Sorted Grid MediumNearest k Values MediumRanked Prime Ratios MediumWords Hidden in a Text MediumTop Three While Typing Medium
loading Python…
Input
shows=[[2, 4, 5], [5, 9, 3], [3, 7, 7]]
Expected
8

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