Longest Increasing Subsequence
来源:互联网 发布:淘宝买东西全部过程 编辑:程序博客网 时间:2024/06/09 21:34
Longest Increasing Subsequence
Problem
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(
nlogn ) time complexity?
题目
给定一个无序的整数数组,求最长递增子序列的长度。
例如:
给定数组 [10, 9, 2, 5, 3, 7, 101, 18],最长递增子序列是 [2, 3, 7, 101],因此,长度为4。注意,最长递增子序列的组合可能不止一种,但只需要求出长度即可。算法的复杂度应该不高于O(
n2 ) 。延伸: 算法时间复杂度能够改进为 O(
nlogn )?
算法发杂度为O(n2 ) 的方法
思路
对于长度为N的数组
即以
算法步骤
假设A为给定的长度为N的数组,L为与数组大小相等的数组。
1. 初始化L的值均为1.
2. i取1到N-1,j取0到i-1,若A[i] > A[j]且L[i] < L[j]+1,则L[i] = L[j]+1。
3. 数组L中的最大值即为数组A的最长递增子序列长度。
代码(C++)
class Solution {public: int lengthOfLIS(vector<int>& nums) { int size = nums.size(); if(size == 0) return 0; vector<int> length(size,1); for(int i = 1;i<size;i++){ for(int j = 0;j<i;j++){ if(nums[i] > nums[j] && length[j]+1 > length[i]){ length[i] = length[j]+1; } } } int maxLength = length[0]; for(int i = 1;i<size;i++){ if(maxLength < length[i]){ maxLength = length[i]; } } return maxLength; }};
- 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
- Python中的二维数组(list与numpy.array)
- IPv4和IPv6
- 汇编学习(三) X86汇编基础(一)
- Java 从jar包中读取资源文件
- Struts2配置问题终极解决方案
- Longest Increasing Subsequence
- IRP的同步完成与异步完成
- 这首王维的古诗该如何解析?
- 互联网+大赛院赛晋级 是喜是忧?
- [Android] 提高ORMLite插入大量数据效率的解决方案
- 缓存学习小结(一)
- 使用google guava做内存缓存
- 《法律备忘》离职是件愉快的事情吗?离职当月工资怎么算?
- iOS APP删除系统相册中选中的图片