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. 32. Longest Valid Parentheses

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  2. (转)MyEclipse +Servlet

    来自:http://www.cnblogs.com/sunada2005/p/3520788.html 在Win7系统下运行自己的第一个Servlet程序,因为有时候配置不当或系统原因可能会运行不成功 ...

  3. Java API ——Collection集合类 & Iterator接口

    对象数组举例: 学生类: package itcast01; /** * Created by gao on 15-12-9. */ public class Student { private St ...

  4. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  5. bzoj1499: [NOI2005]瑰丽华尔兹

    dp. 首先我们可以看到每个时间段只能往一个方向转移最多t步(t为时间段的长度),所以我们可以按时间段dp.因为这个前后值互不影响,也不用占用这一维空间就可以省去. 然后每个时间段内是一列一列(行) ...

  6. Flexigrid自定义显示数据列

    近期在搞ExtJs,发现ExJs的Grid相当的强大,后来又搞Jquery时,就对原来的表格不怎么满意了,于是,花了点时间,从网上找了个Grid插件,这个插件功能是比较强大,什么行排序.筛选.分页都有 ...

  7. codevs 3290 华容道

    HAHAHA BFS+SPFA. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  8. 使用AngularJS 进行Hybrid App 开发已经有一年多时间了,这里做一个总结

    一.AngularJS 初始化加载流程 1.浏览器载入HTML,然后把它解析成DOM.2.浏览器载入angular.js脚本.3.AngularJS等到DOMContentLoaded事件触发.4.A ...

  9. poj 1392 Ouroboros Snake

    题目描述:咬尾蛇是古埃及神话中一种虚构的蛇.它经常把尾巴放在自己的嘴巴里,不停地吞噬自己.环数类似于咬尾蛇,它是2^n位的二进制数,具有如下性质:它能“生成”0-2^n-1之间的所有数.生成方法是:给 ...

  10. 【转】Android Studio简单设置

    原文网址:http://ask.android-studio.org/?/article/14 Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以 ...