class Solution { public: /** * max increasing subsequence * @param arr int整型vector the array * @return int整型 */ int MLS(vector<int>& arr) { sort(arr.begin(), arr.end()); int nowlen = 1, now = arr[0]; int res = 1; for (int i = 1; i < arr.size(); ++i) { if (arr[i] == now + 1) { ++nowlen; now = arr[i]; res = max(res, nowlen); } else if (arr[i] != now) { nowlen = 1; now = arr[i]; } } return res; } };
思路:先排序,然后模拟构造最长连续子序列。