300. Longest Increasing Subsequence
来源:互联网 发布:怎么恢复网络默认设置 编辑:程序博客网 时间:2024/06/14 08:22
Description:
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is [2, 3, 7, 101]
, therefore the length is 4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
Solution:
tails
is an array storing the smallest tail of all increasing subsequences with length i+1
in tails[i]
.
For example, say we have nums = [4,5,6,3]
, then all the available increasing subsequences are:
len = 1 : [4], [5], [6], [3] => tails[0] = 3len = 2 : [4, 5], [5, 6] => tails[1] = 5len = 3 : [4, 5, 6] => tails[2] = 6
We can easily prove that tails is a increasing array. Therefore it is possible to do a binary search in tails array to find the one needs update.
Each time we only do one of the two:
(1) if x is larger than all tails, append it, increase the size by 1(2) if tails[i-1] < x <= tails[i], update tails[i]
Doing so will maintain the tails invariant. The the final answer is just the size.
Java
public int lengthOfLIS(int[] nums) { int[] tails = new int[nums.length]; int size = 0; for (int x : nums) { int i = 0, j = size; while (i != j) { int m = (i + j) / 2; if (tails[m] < x) i = m + 1; else j = m; } tails[i] = x; if (i == size) ++size; } return size;}// Runtime: 2 ms
Python
def lengthOfLIS(self, nums): tails = [0] * len(nums) size = 0 for x in nums: i, j = 0, size while i != j: m = (i + j) / 2 if tails[m] < x: i = m + 1 else: j = m tails[i] = x size = max(i + 1, size) return size# Runtime: 48 ms
int lengthOfLIS(vector<int>& nums) { vector<int> res; for(int i=0; i<nums.size(); i++) { auto it = std::lower_bound(res.begin(), res.end(), nums[i]); if(it==res.end()) res.push_back(nums[i]); else *it = nums[i]; } return res.size();}
Summary:DP方法,也就是O(n^2)方法还是很直接的。O(nLogn)方法没有想到,遂做一记录。
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- 300. Longest Increasing Subsequence
- VM安装Linux(CentOS6.5)及JDK+Tomcat+ MySQL-5.7
- 机器学习基石-03-2-learning with different Data Labels
- 《DOS命令一日通》连载说明
- 求第n个素数
- Oracle ORA-01157: 无法标识/锁定数据文件 解决方法
- 300. Longest Increasing Subsequence
- Android 开发大牛博客
- [贪心] cf883K Road Widening
- 回文问题
- 面试/笔试第三弹 —— 数据库面试问题集锦
- 林轩田机器学习基石及技法课程中线性分类器的总结
- Spring学习笔记——AOP
- 最近的零零碎碎的做了python的笔记以便以后翻阅
- feof多读一次数据的问题