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. Tomcat源代码-门面模式(Facade)

    从Tomcat源码提炼出设计模式-门面设计模式: 概念 外部访问内部,耦合度增加,不利于扩展.而门面模式在内部基础上进行再度封装,只提供外部想要的方法.这时访问方式由“外部---内部”变为了“外部-- ...

  2. JS网站右下角悬浮视窗可关闭广告

    效果体验:http://hovertree.com/texiao/js/4.htm 网站右下角悬浮视窗可关闭广告代码,可收缩.展开,关闭,内容区可自定义html,兼容IE8+.FireFox.Chro ...

  3. django基础篇

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  4. Android开发5:应用程序窗口小部件App Widgets的实现

    前言 本次主要是实现一个Android应用,实现静态广播.动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此次实验的重点是AppWidget小部件的实现啦~ 首先,我 ...

  5. iOS 二维码扫描

    // 导入 AVFoundation.framwork 框架#import "HDCodeViewController.h" #import "HDNormalViewC ...

  6. 邓白氏码的申请-iOS公司开发者账号准备

    相比较个人开发者账号的申请,公司(or企业)的账号申请要稍显复杂,很关键的一点就是邓白氏码的申请,而在网上找了好久也没有找到相对系统的方法流程.因此, 用本文记录我在公司申请邓白氏码过程,以备之后不时 ...

  7. Atitit.木马病毒websql的原理跟个设计

    Atitit.木马病毒websql的原理跟个设计 1. Keyword Wsql { var sql="select "+p.txt+" as t,"+p.v+ ...

  8. 解决Jenkins 2.0 初始化界面卡住的问题

    ***************************************** *原创博客转载请注明出处,谢谢!* **************************************** ...

  9. transform图形变化

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

  10. RESTORE detected an error on page (0:0) in database

    在测试服务器还原生产服务器的一个数据库时遇到了下面错误: System.Data.SqlClient.SqlError: RESTORE detected an error on page (0:0) ...