Two Best Non-Overlapping Events | C++ | Sorting | NlogN
Complexity\n- Time complexity: O(nlogn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\n    int findNextNonOverlapping(vector<vector<int>> &events, int l){\n        int start = 0, end = events.size() - 1, ans = end + 1;\n        while(start <= end){\n            int mid = start + (end - start)/2;\n            if(events[mid][0] > l){\n                ans = mid;\n                end = mid - 1;\n            }else start = mid + 1;\n        }\n        return ans;\n    }\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        int n = events.size();\n        sort(events.begin(), events.end());\n        vector<int> maxi(n, 0);\n        maxi[n-1] = events[n-1][2];\n        for(int i = n-2; i>=0; i--){\n            maxi[i] = max(maxi[i+1], events[i][2]);\n        }\n        int ans = 0;\n        for(int i = 0; i<n; i++){\n            int ind = findNextNonOverlapping(events, events[i][1]);\n            if(ind != n){\n                ans = max(ans, events[i][2] + maxi[ind]);\n            }else ans = max(ans, events[i][2]);\n        }\n        return ans;\n    }\n};\n```
['C++']