✅ Easy to Understand | Intuitive Approach with Binary Search with going Greedy | Detailed Video 🔥
Intuition\nThe problem involves finding the maximum value by selecting one or two non-overlapping events. The key is to leverage the sorted order of events and 
# Intuition\nThe problem involves finding the maximum value by selecting one or two non-overlapping events. The key is to leverage the sorted order of events and use an auxiliary array to quickly access the maximum possible value for subsequent events. This helps efficiently calculate the maximum value by combining two non-overlapping events.\n\n# Approach\n1. **Sorting Events**:  \n   Sort the events by their start time. This ensures that we can use binary search to efficiently find the first valid non-overlapping event.\n\n2. **Suffix Maximum Array**:  \n   Create a `suffixMax` array where each index stores the maximum event value from that index to the end. This allows us to quickly get the maximum value for any valid non-overlapping event.\n\n3. **Iterate Through Events**:  \n   For each event, calculate the maximum value by either:\n   - Only selecting the current event.\n   - Combining the current event\'s value with the maximum value of a valid non-overlapping event (using binary search and `suffixMax`).\n\n4. **Binary Search**:  \n   Use binary search to find the first event whose start time is greater than the end time of the current event. This ensures no overlap between the two events.\n\n5. **Update Maximum Sum**:  \n   Keep track of the maximum value across all possible combinations.\n\n# Complexity\n- **Time Complexity**:  \n  - Sorting the events takes \\(O(n \\log n)\\).  \n  - Creating the `suffixMax` array takes \\(O(n)\\).  \n  - For each event, binary search takes \\(O(\\log n)\\), so iterating through all events takes \\(O(n \\log n)\\).  \n  **Overall**: \\(O(n \\log n)\\).\n\n- **Space Complexity**:  \n  - The `suffixMax` array requires \\(O(n)\\) space.  \n  **Overall**: \\(O(n)\\).\n\n\n# Code\n```java []\nclass Solution {\n    public int maxTwoEvents(int[][] events) {\n        // Step 1: Sort the events by start time\n        Arrays.sort(events, (a, b) -> Integer.compare(a[0], b[0]));\n\n        int n = events.length;\n\n        // Step 2: Create the suffixMax array where suffixMax[i] holds the max value from i to the end\n        int[] suffixMax = new int[n];\n        suffixMax[n - 1] = events[n - 1][2];  // last event\'s value is the base case\n\n        for (int i = n - 2; i >= 0; i--) {\n            suffixMax[i] = Math.max(suffixMax[i + 1], events[i][2]);\n        }\n\n        int maxSum = 0;\n\n        // Step 3: For each event, find the maximum sum of choosing one or two non-overlapping events\n        for (int i = 0; i < n; i++) {\n            // Current event value\n            int currentValue = events[i][2];\n\n            // Step 4: Perform binary search to find the first event whose start time > current event\'s end time\n            int validIndex = binarySearch(events, events[i][1] + 1);\n\n            // If such a valid event exists, add its maximum value from suffixMax array\n            if (validIndex != -1) {\n                currentValue += suffixMax[validIndex];\n            }\n\n            // Update the maximum sum\n            maxSum = Math.max(maxSum, currentValue);\n        }\n\n        return maxSum;\n    }\n\n    // Binary search to find the first event whose start time is greater than the given target time\n    private int binarySearch(int[][] events, int target) {\n        int left = 0, right = events.length - 1;\n        while (left <= right) {\n            int mid = left + (right - left) / 2;\n\n            if (events[mid][0] >= target) {\n                right = mid - 1;\n            } else {\n                left = mid + 1;\n            }\n        }\n\n        // If we find a valid event, return its index; otherwise, return -1\n        return (left < events.length) ? left : -1;\n    }\n\n}\n\n```\n\nLeetCode 2054 Two Best Non Overlapping Events | Binary Search | Greedy | Razorpay Interview Question\nhttps://youtu.be/QBsf9fVFopg
['Array', 'Binary Search', 'Sorting', 'Java']