C++ Memoization code using simple upper bound function || Easy-Understanding
\nint dp[100005][3];\n    vector<int> vec;\n    \n    int func( vector<vector<int>> &events , int i , int choose , int n)\n    {\n        if(i==n)\n        {\n 
```\nint dp[100005][3];\n    vector<int> vec;\n    \n    int func( vector<vector<int>> &events , int i , int choose , int n)\n    {\n        if(i==n)\n        {\n            return 0;\n        }\n        \n        if(choose>=2)\n        {\n            return 0;\n        }\n        \n        if(dp[i][choose]!=-1)\n        {\n            return dp[i][choose];\n        }\n        \n        int val1= func(events , i+1 , choose , n);\n        \n        int idx=upper_bound(vec.begin(),vec.end() , events[i][1])-vec.begin();\n\t\t\n        // upper bound will be implemented in sorted array only \n        // as we have to calculate the starting index of that events[i][1] \n        // so we will make the vec array \n\t\t\n        int val2= events[i][2] + func(events , idx , choose + 1 , n);\n\t\t\n        // This is simple knapsack , take it or not take it \n        // as we need only at max 2 takes , so we make a choose variable \n        // intially choose = 0 , if ( choose >=2)  return 0\n\t\t\n      return  dp[i][choose]=max(val1 , val2);\n    }\n    int maxTwoEvents(vector<vector<int>>& events) {\n         \n        int n=events.size();\n        \n        sort(events.begin(),events.end());\n        \n        for(int i=0;i<events.size();i++)\n        {\n            vec.push_back(events[i][0]);\n        }\n        \n         memset( dp , -1 , sizeof(dp));\n        \n        return func(events ,0 , 0,n);\n        \n        \n    }\n```
['Dynamic Programming', 'Memoization', 'C', 'Binary Tree', 'C++']