import java.util.Arrays;
public class Solution {
public int LIS(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
int[] temp = new int[array.length];
temp[0] = array[0];
int maxLen = 1;
int low, mid, high;
for (int i = 1; i < array.length; i++) {
low = 0;
high = maxLen - 1;
while (low <= high) {
mid = (low + high) >>> 1;
if (array[i] > temp[mid])
low = mid + 1;
else
high = mid - 1;
}
temp[low] = array[i];
if (low == maxLen) {
maxLen++;
}
}
return maxLen;
}
public int lengthOfLIS(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = nums[0];
int len = 1;
for (int i = 1; i < nums.length; i++) {
int pos = Arrays.binarySearch(dp, 0, len, nums[i]);
if (pos < 0) {
pos = -(pos + 1);
dp[pos] = nums[i];
if (pos == len) {
len++;
}
}
}
return len;
}
public static void main(String[] at) {
int[] array = {3, 5, 6, 2, 5, 4, 19, 5, 6, 7, 12};
System.out.println(new Solution().LIS(array));
System.out.println(new Solution().lengthOfLIS(array));
}
}
[参考资料]
http://blog.youkuaiyun.com/dongmianshu/article/details/5954992
https://discuss.leetcode.com/topic/28719/short-java-solution-using-dp-o-n-log-n