2054. Two Best Non-Overlapping Events
UnknownView on LeetCode
Time: O(n log n)
Space: O(n)
Problem Overview
Two Best Non-Overlapping Events (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Binary Search pattern in coding interviews. Sort events by end time; sweep with prefix max value; binary search for non-overlapping pair.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Sort events by end time; sweep with prefix max value; binary search for non-overlapping pair.
Related patterns: Array, Binary Search, Dynamic Programming
2054.cs
C#
// Approach: Sort events by end time; sweep with prefix max value; binary search for non-overlapping pair.
// Time: O(n log n) Space: O(n)
public class Solution
{
public int MaxTwoEvents(int[][] events)
{
int ans = 0;
int maxValue = 0;
Event[] evts = new Event[events.Length * 2];
for (int i = 0; i < events.Length; ++i)
{
int start = events[i][0];
int end = events[i][1];
int value = events[i][2];
evts[i * 2] = new Event(start, value, 1);
evts[i * 2 + 1] = new Event(end + 1, value, 0);
}
Array.Sort(evts, (a, b) =>
a.time == b.time ? a.isStart.CompareTo(b.isStart) : a.time.CompareTo(b.time));
foreach (Event evt in evts)
{
if (evt.isStart == 1)
ans = Math.Max(ans, evt.value + maxValue);
else
maxValue = Math.Max(maxValue, evt.value);
}
return ans;
}
}
public class Event
{
public int time;
public int value;
public int isStart;
public Event(int time, int value, int isStart)
{
this.time = time;
this.value = value;
this.isStart = isStart;
}
}
Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 22. Generate Parentheses(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)