LintCode-Longest Increasing Subsequence
Given a sequence of integers, find the longest increasing subsequence (LIS).
You code should return the length of the LIS.
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4
Time complexity O(n^2) or O(nlogn)
What's the definition of longest increasing subsequence?
* The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
Solution 1 (nlogn):
public class Solution {
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
public int longestIncreasingSubsequence(int[] nums) {
if (nums.length==0) return 0;
int len = nums.length;
int[] seqEnd = new int[len+1];
seqEnd[1] = 0;
int lisLen = 1;
for (int i=1;i<len;i++){
int pos = findPos(nums,seqEnd,lisLen,i);
seqEnd[pos] = i;
if (pos>lisLen) lisLen = pos;
} return lisLen; } public int findPos(int[] nums, int[] seqEnd, int lisLen, int index){
int start = 1;
int end = lisLen;
while (start<=end){
int mid = (start+end)/2; if (nums[index] == nums[seqEnd[mid]]){
return mid;
} else if (nums[index]>nums[seqEnd[mid]]){
start = mid+1;
} else end = mid-1;
}
return start;
}
}
Solution 2 (n^2 DP):
public class Solution {
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
public int longestIncreasingSubsequence(int[] nums) {
if (nums.length==0) return 0;
int len = nums.length;
int[] lisLen = new int[len];
lisLen[0] = 1;
int maxLen = lisLen[0];
for (int i=1;i<len;i++){
lisLen[i]=1;
for (int j=i-1;j>=0;j--)
if (nums[i]>=nums[j] && lisLen[i]<lisLen[j]+1)
lisLen[i] = lisLen[j]+1;
if (maxLen<lisLen[i]) maxLen = lisLen[i];
} return maxLen; }
}
LintCode-Longest Increasing Subsequence的更多相关文章
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
- LintCode刷题笔记--Longest Increasing Subsequence
标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
随机推荐
- hibernate(2) —— 主键策略
框架提供了三种主键生成方式,一种是由用户自己维护,一种是由hibernate框架维护,另一种是由数据库维护. 自己维护就是在插入数据的时候,一定要指定主键的值,否则会出错,如果由框架维护和由数据库维护 ...
- SharePoint 客户端对象模型共用ClientContext的坑
首先请看代码 private static void Main(string[] args) { Test2(); } private static void Test2() { var client ...
- sharePoint 2016 弃用和删除的功能
前言 sharepoint 2016版本正式发布,但是相比较2013版本,还是删除或者准备删除一些功能,我们需要了解一下哪些功能已经被删除掉或者在下一个版本中移除,因为这些可能影响我们现有系统是否能够 ...
- [Android]对MVC和MVP的总结
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5036289.html 经历过的客户端的架构分为这么几个阶段: ...
- Android的Message Pool是什么——源码角度分析
原文地址: http://blog.csdn.net/xplee0576/article/details/46875555 Android中,我们在线程之间通信传递通常采用Android的消息机制,而 ...
- vim中tab转为空格
:set ts=4:set expandtab:%retab!
- PL/SQL Developer连接本地Oracle 11g 64位数据库
转摘:http://www.cnblogs.com/ymj126/p/3712727.html 用于学习,笔记,以备后用. 1.登录PL/SQL Developer 这里省略Oracle数据库和PL/ ...
- 浅谈iptables防SYN Flood攻击和CC攻击
------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...
- PostgreSQL-角色、库、模式、表
由于不了解postgresql的psql工具,安装完数据库后就直接用pgadmin或navicat来连接操作,在确认初始化后的库中默认有些什么东西后竟然一直无处下手,在还没有了解pg大致体系的情况下搞 ...
- qt5.4.0编译错误
error1: 进程"C:\Qt\Qt5.4.0\Tools\QtCreator\bin\jom.exe"退出,退出代码 2 solution:去工具->选项->构建和 ...