标签:date 技术 load turn com png ati leetcode 区间
本题思路是使相同长度下序列增长要尽可能慢
package leetCode.动态规划;
/**
* @author km
* @date 2021年06月11日
**/
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
LongestIncreasingSubsequence main = new LongestIncreasingSubsequence();
System.out.println(main.lengthOfLIS(new int[]{4,10,4,3,8,9}));
}
public int lengthOfLIS(int[] nums) {
int length = nums.length;
int[] tail = new int[length + 1];
tail[1] = nums[0];
int len = 1;
for (int i = 1; i < length; i++) {
if (nums[i] > tail[len]) {
tail[++len] = nums[i];
} else {
int left = 1, right = len, pos = 0;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (tail[mid] >= nums[i]) {
right = mid - 1;
pos = right;
} else {
left = mid + 1;
}
}
tail[pos + 1] = nums[i];
}
}
return len;
}
}
在使用二分法的时候,最后需要返回的位置应该如何思考?
标签:date 技术 load turn com png ati leetcode 区间
原文地址:https://www.cnblogs.com/kmchen/p/14875922.html