Leetcode Problem 2054. Two Best Non-Overlapping Events

2054. Two Best Non-Overlapping Events

Leetcode Solutions

Sort and Greedy Approach

  1. Create a list proc to store the start and end times of events along with their values.
  2. For each event, add two tuples to proc: one for the start time and one for the end time (incremented by 1 to account for inclusivity).
  3. Sort the proc list by time.
  4. Initialize ans to 0 and m to 0, where m represents the maximum value of any event that has ended so far.
  5. Iterate over the sorted proc list: a. If the current tuple represents the start of an event, update ans to be the maximum of ans and m + val. b. If the current tuple represents the end of an event, update m to be the maximum of m and val.
  6. Return ans as the maximum sum of two non-overlapping events.
UML Thumbnail

Dynamic Programming with Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...