Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Example

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

Challenge

Time complexity O(n^2) or O(nlogn)

Clarification

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的更多相关文章

  1. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  2. [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

    Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...

  3. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  4. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  7. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  8. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  10. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

随机推荐

  1. jquery和css3实现滑动导航菜单

    效果预览:http://keleyi.com/keleyi/phtml/html5/15/ 有都中颜色可供选择,请使用支持HTML5/CSS3的浏览器访问. HTML源代码: <!DOCTYPE ...

  2. 原生JS:全局属性、全局方法详解

    全局属性.全局方法 原创文章,转摘请注明出处:苏福:http://www.cnblogs.com/susufufu/p/5853342.html 首先普及几个我总结的非常实用又很基础的知识:(呵呵,仅 ...

  3. 主成分分析(principal components analysis, PCA)

    原理 计算方法 主要性质 有关统计量 主成分个数的选取 ------------------------------------------------------------------------ ...

  4. SharePoint 2013 新建网站集图解

    前言:接触SharePoint的人可能是越来越多,但是很多人一接触就很迷茫,在技术群里问如何新建网站集,这样一篇图解,帮助新手学习在搭建好SharePoint环境之后,如何创建一个网站集,做一个基本的 ...

  5. 视图xsl定制之嵌入服务器控件

    SharePoint 2010 视图 xsl 文件中支持嵌入服务器控件,嵌入服务器控件时,系统先采用xsl将视图xml解析成一个类似UserControl的存在,然后执行UserControl. 代码 ...

  6. Github+hexo绑定域名

    Github绑定域名 近期在新网购买了一个属于自己的域名,因此想着把自己用hexo+github搭建的博客通过域名访问,但是找了n长时间来搞,都没有成功.心灰意冷之中再次通过google来搜索,终于有 ...

  7. linux数据误删后,灾难性数据备份与数据还原

    一 准备工作 #rm –rf  误删重要数据怎么办? 1. 要冷静,通知停止该服务器一切操作 2. 查看被删除文件所在分区 #mount 3. 将该分区设置为只读 #mount -r -n -o re ...

  8. 学习Maven之Properties Maven Plugin

    1.properties-maven-plugin是个什么鬼? 介绍前我们先看一个问题,比如我们有一个maven项目结构如下: 一般我们都把一些配置文件放到像src/main/resources/jd ...

  9. PostgreSQL-join多表连接查询和子查询

    一.多表连接查询 1.连接方式概览 [inner] join 内连接:表A和表B以元组为单位做一个笛卡尔积,记为表C,然后在C中挑选出满足符合on 语句后边的限制条件的内容. left [outer] ...

  10. 修改AndroidStudio中的Logcat中的默认设置

    按Ctrl+Alt+S打开Settings 在左上角的输入框中输入Locat 点右边的使用内部样式取消掉,好了,开始自定义吧.