C++ | DP Memorization + Binary Search | 60% Fast | Easy to Follow Code
Please Upvote :)\n\n\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(),events.end());\n        vector
Please Upvote :)\n\n```\nclass Solution {\npublic:\n    int maxTwoEvents(vector<vector<int>>& events) {\n        sort(events.begin(),events.end());\n        vector<vector<int>> dp(events.size(),vector<int>(2,-1));\n        return fun(events,dp,0,0);\n    }\n    int fun(vector<vector<int>>& v,vector<vector<int>>& dp,int i,int t)\n    {\n        if(t==2 || i>=v.size())\n            return 0;\n        \n        if(dp[i][t]!=-1)\n            return dp[i][t];\n        \n        int res=fun(v,dp,i+1,t);\n        int next=search(v,v[i][1],i);\n        res=max(res,v[i][2]+fun(v,dp,next,t+1));\n        \n        return dp[i][t]=res;\n    }\n    int search(vector<vector<int>>& v,int val,int i)\n    {\n        int l=i+1;\n        int r=v.size()-1;\n        int res=v.size();\n        while(l<=r)\n        {\n            int mid=(l+r)/2;\n            if(v[mid][0]>val)\n            {\n                res=mid;\n                r=mid-1;\n            }\n            else\n                l=mid+1;\n        }\n        return res;\n    }\n};\n```
['Dynamic Programming', 'C', 'Sorting', 'Binary Tree', 'C++']