标签:first vat 子序列 严格 for else public 最长递增子序列 fun
class Solution {
private static int upperBound(int[] s, int len, int k) {
int first = 0;
while (len > 0) {
int half = len >>> 1;
int m = first + half;
// 非严格递增s[m] <= k,严格递增s[m] < k
if (s[m] <= k) {
len -= half + 1;
first = m + 1;
} else {
len = half;
}
}
return first;
}
private static int function(int[] x) {
if (x == null || x.length == 0) {
return 0;
}
int[] dp = new int[x.length];
int max = 1;
dp[0] = x[0];
for (int i = 1;i < x.length; ++ i) {
int index = upperBound(dp, max, x[i]);
if (index < max) {
dp[index] = x[i];
} else {
dp[max ++] = x[i];
}
}
return max;
}
public int lengthOfLIS(int[] nums) {
return function(nums);
}
}
标签:first vat 子序列 严格 for else public 最长递增子序列 fun
原文地址:http://blog.51cto.com/tianyiya/2341777