C++ || Recursion + Memoization || Easy Understanding
\n// Basically my intuition is sort of similar like Knapsack picking.\n// We have two options : Pick Event or Don\'t Pick an event.\n// If we "Pick event", we\'
```\n// Basically my intuition is sort of similar like Knapsack picking.\n// We have two options : Pick Event or Don\'t Pick an event.\n// If we "Pick event", we\'ll just add to our sum, then we\'ll find out an event whose starting time > ending time of the event that we picked and \n// we\'ll keep progressing like this until we hit 2. (as we can only pick at most two events)\n// If we "Don\'t Pick the event" we\'ll move on\n// And basically our maxvalue would be max outcome of ("Pick Event", "Don\'t Pick Event")\n\nclass Solution {\npublic:\n    \n    //Main Function\n    int maxTwoEvents(vector<vector<int>>& events) {\n        int n=events.size();\n        vector<vector<int>>dp(n,vector<int>(2,-1));\n        \n        //Sorting because since we need to find an event that has starting time > ending time \n        //of previous event selected, so applying binary search would help there.\n        sort(events.begin(),events.end());\n        \n        return solve(events,0,0,dp);\n    }\n    \n    //Helper function\n    int solve(vector<vector<int>>&nums,int idx,int k,vector<vector<int>>&dp)\n    {\n        // Base case\n        if(k==2)\n        {\n            return 0;\n        }\n        if(idx>=nums.size())\n        {\n            return 0;\n        }\n        \n        // Memoization check\n        if(dp[idx][k]!=-1)\n        {\n            return dp[idx][k];\n        }\n        \n        //Basically ending times of the events\n        vector<int>ans={nums[idx][1],INT_MAX,INT_MAX};\n        \n        //Searching the event whose starting time > ending time of previous event selected\n        int nextindex=upper_bound(begin(nums),end(nums),ans)-begin(nums);\n        \n        //Pick event\n        int include=nums[idx][2]+solve(nums,nextindex,k+1,dp);\n        \n        //Don\'t Pick event\n        int exclude=solve(nums,idx+1,k,dp);\n        \n        return dp[idx][k]=max(include,exclude); //Max of(Pick, Not Pick)\n    }\n};\n```
['Memoization', 'C', 'Binary Tree']