并没有用上并查集,很简单的一道题,但是如果是考试模式,容易忽略连续序列里面相等的情况
int cmp(const void *a, const void *b) { int aa = *(int *)a; int bb = *(int *)b; return aa - bb; } int MLS(int* arr, int arrLen ) { int num = 1; int res = 0; qsort(arr, arrLen, sizeof(arr[0]), cmp); for (int i = 1; i < arrLen; i++) { if (arr[i] - arr[i-1] == 0) continue; if (arr[i] - arr[i-1] == 1) { num++; } else { num = 1; } res = fmax(num, res); } return res; }