C++ DP version, Time O(N^2) Space O(N)
   bool cmp (pair<int, int> i, pair<int, int> j) {\n            if (i.first == j.first)\n                return i.second < j.second;\n            return i.first
       bool cmp (pair<int, int> i, pair<int, int> j) {\n            if (i.first == j.first)\n                return i.second < j.second;\n            return i.first < j.first;\n        }\n        \n        class Solution {\n        public:\n            int maxEnvelopes(vector<pair<int, int>>& envelopes) {\n                int N = envelopes.size();\n                vector<int> dp(N, 1);\n                int mx = (N == 0) ? 0 : 1;\n                sort(envelopes.begin(), envelopes.end(), cmp);\n                for (int i = 0; i < N; i++) {\n                    for (int j = i - 1; j >= 0; j--) {\n                        if (envelopes[i].first > envelopes[j].first && envelopes[i].second > envelopes[j].second) {\n                            dp[i] = max(dp[i], dp[j] + 1);\n                            mx = max(dp[i], mx);\n                        }\n                    }\n                }\n                return mx;\n            }\n        };
[]