Longest Increasing Subsequence
来源:互联网 发布:通信概预算软件 编辑:程序博客网 时间:2024/06/10 04:32
题目:
给定一个没有排好序的数组,求一个其递增的子序列,返回长度。
使用O(nlogn)的解法。
解题思路:
一般来说,要求使用O(nlogn)时间复杂度的,首先想到二分查找,设置一个非递减的数组。
具体思路看下面的例子。
代码:
public int lengthOfLIS(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int[] min = new int[nums.length + 1]; min[0] = Integer.MIN_VALUE; for (int i = 1; i < min.length; ++i) { min[i] = Integer.MAX_VALUE; } for (int i = 0; i < nums.length; ++i) { int index = binarySearch(min, nums[i]); min[index] = nums[i]; } for (int i = nums.length; i >= 1; --i) { if (min[i] != Integer.MAX_VALUE) { return i; } } return 0; } //找到大于num的第一个数下标 private int binarySearch(int[] minLast, int num) { int start =0; int end = minLast.length - 1; int mid; while (start + 1 < end) { mid = start + ((end - start) >> 1); if (minLast[mid] < num) { start = mid; } else { end = mid; } } if (minLast[start] > num) { return start; } return end; }
-----------EOF----------
0 0
- Longest Increasing Subsequence
- 【DP】 Longest Increasing Subsequence
- Longest Increasing Subsequence(LIS)
- Longest Increasing Subsequence
- 【算法】Longest Increasing Subsequence
- Longest Increasing Subsequence
- Longest Increasing Subsequence
- Longest Increasing Subsequence
- Longest Increasing Subsequence
- Longest Increasing Subsequence(LIS)
- Longest Increasing Subsequence
- [DP]Longest Increasing Subsequence
- The Longest increasing subsequence
- [刷题]Longest Increasing Subsequence
- longest-increasing-subsequence
- Longest Increasing Subsequence
- codeforces568E.Longest Increasing Subsequence
- Longest Increasing Subsequence
- OpenGl 的BeginMode 和 shader
- 实战录 | AngularJS入坑记
- js判断浏览器是否为ie
- eclipse 导入myeclipse web项目
- codeforces 743 D. Chloe and pleasant prizes(DFS)
- Longest Increasing Subsequence
- Fragment与Activity之间的相互通信
- Codeforces Round #384 (Div. 2) B. Chloe and the sequence 找规律
- ctrl键看android studio java源码
- ubuntu下eclipse配置opencv
- java日期格式yyyy-MM-dd HH:mm:ss该如何记忆?
- 第16周项目1 验证算法(2)希尔排序
- jar解压缩、再压缩(permission denied出错解决)
- iOS监听H5页面goBack返回事件问题