public class Longest_Increasing_Subsequence {
/**
* O(N^2)
* DP
* 思路:
* 示例:[1,0,2,4,10,5]
* 找出以上数组的LIS的长度
* 分析:
* 只要求长度,并不要求找出具体的序列
* 问题可以拆分为
* 1. 对于[1],找出LIS
* 2. 对于[1,0],找出LIS
* 3. 对于[1,0,2],找出LIS
* 4. 对于[1,0,2,4],找出LIS
* ...
* 最后,对于[1,0,2,4,10,5],找出LIS
* 再进一步思考,例如:
* 找出[-1,0,1,0,2,4]的LIS,就要找到在4之前符合条件(都比4小且都为升序)的LIS的长度 => [-1,0,1]是满足情况的(最长,都是升序,都比4小)
* 那么就要有一个数据结构来记录到某一个index上,LIS的长度。因为每一个index上的LIS长度并不是固定为前一个加1,所以每一个都要记录下来 => 数组dp[]
* dp[i]记录的是,在i这个index上,LIS的长度
* 比如:
* index 0 1 2 3 4 5
* dp:[ 1,2,3,1,4,5] //dp数组
* ar:[-1,0,1,0,2,4] //原数组
* dp[1] = 2表示在1这个index上,LIS的长度是2([-1,0])
* dp[4] = 4表示在4这个index上,LIS的长度是4([-1,0,1,2])
* ----------------------------
* 状态转换方程:
* dp[i] = dp[k] + 1; (dp[k] = max(dp[0], dp[1], ... dp[i-1])) // dp[i] = 在i以前最大的LIS长度加上1
* 以上方程的成立条件:
* nums[k] < nums[i] //保持递增序列的属性
*/ /**
* O(N^2)
*/
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = 1;
for (int i = 1; i < nums.length; i++) {
int beforeMaxLen = dp[i];
// 在0 ~ i之间比较LIS的长度
for(int j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] > beforeMaxLen) { //注意dp[j] > beforeMaxLen,新的长度要大于之前选出来的长度才能更新
beforeMaxLen = dp[j];
}
}
dp[i] = beforeMaxLen + 1;
}
int max = 0;
// 在数组里找出最大的长度即可
for (int i = 0; i < nums.length; i++) {
if (dp[i] > max){
max = dp[i];
}
}
return max;
} /**
* O(N*logN)
* 思路:
* 满足递增序列,就直接加入list中
* 如果发现有降序出现,找出在原数组中比它大的第一个数的index,然后在list中替换那个数
* 最后返回list的长度
* 原理:
* 因为只求长度,所以没有必要存储确切的sequence
*/
public int lengthOfLIS_2(int[] nums) {
List<Integer> list = new ArrayList<>();
for(int num : nums) {
if(list.isEmpty() || list.get(list.size() - 1) < num) { // 不满足递增序列
list.add(num);
} else {
list.set(findFirstLargeEqual(list, num), num);
}
} return list.size();
} private int findFirstLargeEqual(List<Integer> list, int target)
{
int start = 0;
int end = list.size() - 1;
while(start < end) {
int mid = start + (end - start) / 2;
if(list.get(mid) < target) {
start = mid + 1;
}
else {
end = mid;
}
} return end;
} /**
* 测试用
*/
public static void main(String[] args) {
Longest_Increasing_Subsequence lis = new Longest_Increasing_Subsequence();
int[] a = {-1,0,1,0,2,4};
System.out.print(lis.lengthOfLIS_2(a));
}
}

Longest Increasing Sequence的更多相关文章

  1. 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)

    分析: 完整 代码: // 最长不下降子序列 #include <stdio.h> #include <algorithm> using namespace std; ; in ...

  2. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

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

  3. CSUOJ 1551 Longest Increasing Subsequence Again

    1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 75  Solved ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  6. The Longest Increasing Subsequence (LIS)

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

  7. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  9. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

随机推荐

  1. SQL Server数据库多种方式查找重复记录

    摘要:SQL Server是一个关系数据库管理系统,SQL Server数据库的应用是很多的,SQL Server数据库赢得了广大用户的青睐,本文将主要为大家介绍关于SQL Server数据库中查找重 ...

  2. 实例学习写Makefile文件

    目录 0. 扫盲 1. 编译,链接 2. Makefile文件执行 3. Makefile书写规则 4. 案例 5. Makefile是如何工作的 6. 拔高,参考 0. 扫盲 Linux 环境下的程 ...

  3. windows和linux共享文件

    一篇文章: 环境:主机操作系统 是Windows XP ,虚拟机 是Ubuntu 9.10,虚拟机是VirtualBox 3.08. 1. 安装增强功能包(Guest Additions) 安装好Ub ...

  4. math模块及使用方式

    在数学之中,除了加减乘除四则运算之外——这是小学数学——还有其它更多的运算,比如乘方.开方.对数运算等等,要实现这些运算,需要用到 Python 中的一个模块:Math 模块(module)是 Pyt ...

  5. option配置

    wildignore:用来设置忽略的文件匹配模式,shell模式

  6. Visaul Studio2015安装以及c++单元测试使用方法

    Visual Studio 2015安装流程     vs2015是一款十分好用的IDE,接下来就介绍一下安装流程.这里采用在线安装方式,从官网下载使得安装更加安全. 第一步:在百度中搜索Visual ...

  7. ifdebug

    #if DEBUG 首先,大小写不能写错. 其次,解决方案配置设为:Debug,才会执行该语句,如果在条件里面搭配Debug.Assert等,效果甚佳.而如果要设置为Release模式,就不会执行条件 ...

  8. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  9. 使用netcat进行反弹链接的shellcode

    from:http://morgawr.github.io/hacking/2014/03/29/shellcode-to-reverse-bind-with-netcat/ 这篇文章主要是谈,在远程 ...

  10. 高斯消元 分析 && 模板 (转载)

    转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...