C# Solution for Two Best Non-Overlapping Events Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to find the maximum sum of values from two non-overlapping events. Inste
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to find the maximum sum of values from two non-overlapping events. Instead of comparing every possible pair of events (which would be inefficient), we break events into start and end points and process them in chronological order. This way:\n\u2022\tWe can dynamically keep track of the maximum value of a single event encountered so far.\n\u2022\tWhenever a new event starts, we check if combining it with the best event seen earlier produces a new maximum sum.\n\nThis approach avoids brute-forcing all pairs, leveraging sorted timelines for efficiency\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tTimeline Creation:\n\t\u2022\tFor each event [start, end, value], add two timeline points:\n\t\u2022\t(start, 1, value) for when the event starts.\n\t\u2022\t(end + 1, 0, value) for when the event ends (since the end time is inclusive).\n\t\u2022\tThis splits events into discrete points for easier processing.\n2.\tSorting:\n\t\u2022\tSort the timeline points by time:\n\t\u2022\tPrimary: Time value.\n\t\u2022\tSecondary: Prioritize end points (type == 0) over start points (type == 1) when the times are equal.\n3.\tSweeping Through Points:\n\t\u2022\tUse two variables:\n\t\u2022\tmaxValue: Tracks the maximum single-event value seen so far.\n\t\u2022\tans: Tracks the maximum sum of values of two non-overlapping events.\n\t\u2022\tProcess each point:\n\t\u2022\tFor start points (type == 1): Check if the sum of the current event\u2019s value and maxValue is greater than ans, and update ans if so.\n\t\u2022\tFor end points (type == 0): Update maxValue to include the current event\u2019s value if it\u2019s the largest seen so far.\n4.\tResult:\n\t\u2022\tAfter processing all points, ans contains the maximum sum of two non-overlapping events.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n1.\tTimeline Construction:\n\t\u2022\tSplitting each event into two points takes O(n), where n is the number of events.\n2.\tSorting:\n\t\u2022\tSorting the timeline points takes O(2n log (2n)) = O(n log n), as there are 2n points.\n3.\tSweeping Through Points:\n\t\u2022\tProcessing all timeline points takes O(2n) = O(n).\n\nOverall: O(n log n), dominated by the sorting step.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n1.\tAdditional Data Structures:\n\t\u2022\tThe times list stores 2n points: O(n).\n2.\tAuxiliary Variables:\n\t\u2022\tA constant amount of space is used for variables like maxValue and ans.\n\nOverall: O(n).\n\n# Code\n```csharp []\npublic class Solution {\n    public int MaxTwoEvents(int[][] events) {\n        var times = new List<int[]>();\n\n        foreach (var eventData in events) {\n            int start = eventData[0];\n            int end = eventData[1];\n            int value = eventData[2];\n\n            times.Add(new int[] { start, 1, value }); \n            times.Add(new int[] { end + 1, 0, value }); \n        }\n\n        times.Sort((a, b) => a[0] == b[0] ? a[1].CompareTo(b[1]) : a[0].CompareTo(b[0]));\n\n        int maxValue = 0;\n        int ans = 0;\n        foreach (var time in times) {\n            if (time[1] == 1) {\n                ans = Math.Max(ans, time[2] + maxValue);\n            } else {\n                maxValue = Math.Max(maxValue, time[2]);\n            }\n        }\n\n        return ans;\n    }\n}\n```
['C#']