The best answer your'e looking for. RECURSION|MEMOIZATION|TABULATION|1D OPTIMIZATION| BINARY SEARCH
Recursion (gives TLE at test 57)\n\nclass Solution {\npublic:\n    int f(int curr,int prev,vector<vector<int>>& envelopes){\n        if(curr==envelopes.size()){
# Recursion (gives TLE at test 57)\n```\nclass Solution {\npublic:\n    int f(int curr,int prev,vector<vector<int>>& envelopes){\n        if(curr==envelopes.size()){\n            return 0;\n        }\n        int notTake=0+f(curr+1,prev,envelopes);\n        int take=-1e9;\n        if(prev==-1 or (envelopes[prev][0]<envelopes[curr][0] and envelopes[prev][1]     <envelopes[curr][1])){\n                take=1+f(curr+1,curr,envelopes);\n        }\n        return max(take,notTake);\n    }\n    int maxEnvelopes(vector<vector<int>>& envelopes) {\n        sort(envelopes.begin(),envelopes.end());\n        return f(0,-1,envelopes);\n    }\n};\n```\n\n# Memoization (gives TLE at test 83)\n```\nclass Solution {\npublic:\n    int f(int curr,int prev,vector<vector<int>>& envelopes,vector<vector<int>> &dp){\n        if(curr==envelopes.size()){\n            return 0;\n        }\n        if(dp[curr][prev+1]!=-1){\n            return dp[curr][prev+1];\n        }\n        int notTake=0+f(curr+1,prev,envelopes,dp);\n        int take=-1e9;\n        if(prev==-1 or (envelopes[prev][0]<envelopes[curr][0] and envelopes[prev][1]     <envelopes[curr][1])){\n                take=1+f(curr+1,curr,envelopes,dp);\n        }\n        return dp[curr][prev+1]=max(take,notTake);\n    }\n    int maxEnvelopes(vector<vector<int>>& envelopes) {\n        sort(envelopes.begin(),envelopes.end());\n        int n=envelopes.size();\n        vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n        return f(0,-1,envelopes,dp);\n    }\n};\n```\n\n# Tabulation (gives TLE at test 85)\n```\nclass Solution {\npublic:\n    int maxEnvelopes(vector<vector<int>>& envelopes) {\n        sort(envelopes.begin(),envelopes.end());\n        int n=envelopes.size();\n        vector<vector<int>> dp(n+1,vector<int>(n+1,0));\n        for(int curr=n-1;curr>=0;curr--){\n            for(int prev=curr-1;prev>=-1;prev--){\n                int notTake=0+dp[curr+1][prev+1];\n                int take=-1e9;\n                if(prev==-1 or (envelopes[prev][0]<envelopes[curr][0] and envelopes[prev][1]<envelopes[curr][1])){\n                        take=1+dp[curr+1][curr+1];\n                }\n             dp[curr][prev+1]=max(take,notTake); \n            }\n        }\n        return dp[0][0];\n          \n    }\n};\n```\n\n# 1D array optimization (gives TLE at test 85)\n```\nclass Solution {\npublic:\n    int maxEnvelopes(vector<vector<int>>& envelopes) {\n        sort(envelopes.begin(),envelopes.end());\n        int n=envelopes.size();\n        vector<int>next(n+1,0),currr(n+1,0);\n        for(int curr=n-1;curr>=0;curr--){\n            for(int prev=curr-1;prev>=-1;prev--){\n                int notTake=0+next[prev+1];\n                int take=-1e9;\n                if(prev==-1 or (envelopes[prev][0]<envelopes[curr][0] and envelopes[prev][1]<envelopes[curr][1])){\n                        take=1+next[curr+1];\n                }\n             currr[prev+1]=max(take,notTake); \n            }\n            next=currr;\n        }\n        return currr[-1+1];\n    }\n};\n```\n\n# Binary Search (Final asnwer)\n```\nclass Solution {\npublic:\n    static bool comp(vector<int> &a,vector<int>&b){\n        if(a[0]==b[0]){\n            return a[1]>b[1];\n        }\n        else{\n            return a[0]<b[0];\n        }\n    }\n    int maxEnvelopes(vector<vector<int>>& envelopes) {\n        sort(envelopes.begin(),envelopes.end(),comp);\n        vector<int> temp;\n        temp.push_back(envelopes[0][1]);\n        for(int i=1;i<envelopes.size();i++){\n            if(temp.back()<envelopes[i][1]){\n                temp.push_back(envelopes[i][1]);\n            }\n            else{\n                auto ind=lower_bound(temp.begin(),temp.end(),envelopes[i][1])-temp.begin();\n                temp[ind]=envelopes[i][1];\n            }\n        }\n        return temp.size();\n    }\n};\n```
['Binary Search', 'Recursion', 'Memoization', 'C++']