LeetCode solutions

2164. Two Best Non-Overlapping Events

My accepted Python solution to LeetCode problem 2164, Two Best Non-Overlapping Events, running in 310ms.

  • Difficulty: Medium
  • Python
  • Runtime 310ms
  • Memory 46MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 310ms, memory 46MB, accepted 2025-12-23.

python
class Solution:
    def maxTwoEvents(self, events):
        import bisect
        events.sort(key=lambda e: e[1])
        n = len(events)
        max_val = [0] * n
        max_val[0] = events[0][2]
        for i in range(1, n):
            max_val[i] = max(max_val[i-1], events[i][2])
        result = max_val[n-1]
        end_times = [e[1] for e in events]
        for i in range(n):
            start = events[i][0]
            idx = bisect.bisect_left(end_times, start) - 1
            if idx >= 0:
                result = max(result, events[i][2] + max_val[idx])
        return result

Source