牛客地址
实现代码
import java.util.*;
public class Solution {
/**
* max increasing subsequence
* @param arr int整型一维数组 the array
* @return int整型
*/
public int MLS (int[] arr) {
// write code here
if(arr == null || arr.length == 0){
return 0;
}
Set<Integer> set = new HashSet<>();
for(int num : arr){
set.add(num);
}
int res = Integer.MIN_VALUE;
for(int num : arr){
if(!set.contains(num - 1)){
int tmp = num + 1;
int count = 1;
while(set.contains(tmp)){
count ++;
tmp ++;
}
res = Math.max(res, count);
}
}
return res;
}
}