最长递增子序列 nlogn
2016-06-09 22:01
316 查看
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.csdn.net/dongmianshu/article/details/5954992
https://discuss.leetcode.com/topic/28719/short-java-solution-using-dp-o-n-log-n
相关文章推荐
- 求数组中最长递增子序列的解决方法
- PB开发还有前途吗
- 最长上升子序列(LIS)(HDU 1025)
- DP-LIS( Longest Increasing Subsequenc)
- 最长递增子序列(LIS)的两种实现
- 又见拦截导弹
- 动态规划求一道 类最长递增公共子序列
- 动态规划求一道 类最长递增公共子序列
- hdu1267最少拦截系统
- 最长递增子序列
- 最长递增子序列
- hdu 1025 最长严格上升子序列
- 【转】最大和子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
- hdu1231lis+起始位置和终点位置
- hdu1025Constructing Roads In JGShining's Kingdom(最长递增子序列)
- 最长递增子序列
- codeforces#323(div2) D. Once Again... 最长不下降子序列
- 第七周作业3 最长递增子序列
- 最长递增子序列的应用
- deque list vector 区别