Maximum Number of Events That Can Be Attended II

HardDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given events where events[i] = [startDay_i, endDay_i, value_i]: attending event i means being there for every day from startDay_i to endDay_i **inclusive**, and it earns value_i. You can attend at most k events. You can only be at one event per day, and you must attend an event completely, so two chosen events may not share any day. In particular, an event ending on day d and another starting on day d cannot both be chosen. Return the maximum total value you can earn.

Examples

Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
Output: 7
Attend [1,2] (4) and [3,4] (3).
Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
Output: 10
[2,3] overlaps both others, but its value 10 beats 4 + 3.
Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
Output: 9
Pick the three most valuable non-overlapping events: 2 + 3 + 4.

Constraints

  • 1 <= k <= events.length
  • 1 <= k * events.length <= 10^6
  • 1 <= startDay_i <= endDay_i <= 10^9
  • 1 <= value_i <= 10^6

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems