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 ... 
随机推荐
- (原)3.3 Zookeeper应用 - 负载均衡
			本文为原创文章,转载请注明出处,谢谢 负载均衡 1.原理 服务端启动创建临时节点(下图中servers下节点),临时节点数据包含负载信息 客户端启动获取服务器列表,并根据负载去连接一个负载较轻的服务器 ... 
- 仿腾讯QQ竖直滑动导航菜单
			菜单就像qq软件的分组,鼠标经过自动显示相应组的内容. 效果体验网址:http://keleyi.com/a/bjad/nf86w2dv.htm 以下是源代码: <html> <he ... 
- SharePoint 2013 工作流平台的选项不可用
			问题描述 当我想创建一个SharePoint 2013 工作流的时候,打开SharePoint 2013 Designer(一下简称SPD),发现没有SharePoint 2013 工作流的选项.原来 ... 
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q138-Q140)
			Question 138 You are designing a SharePoint 2010 application that will deploy a Web Part assembly. ... 
- Android Gson的使用总结
			1.概念 Gson是谷歌发布的一个json解析框架 2.如何获取 github:https://github.com/google/gson android studio使用 compile 'com ... 
- IOS开发基础知识--碎片45
			1:iOS SEL的简单总结 SEL就是对方法的一种包装.包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法 a.方法的存储位置 在内存中每个类的方法都存储在类对象中 每个方法都有一 ... 
- iOS真机测试碰到错误linker command failed with exit code 1 (use -v to see invocation)
			在模拟器上运行正常,但是在模拟器上就会报错,这是因为xocde7之后增加了一个bitcode,bitcode是被编译程序的一种中间形式的代码.包含bitcode配置的程序将会在App store上被编 ... 
- iOS中的round/ceil/floorf函数略解
			extern float ceilf(float); extern double ceil(double); extern long double ceill(long double); extern ... 
- python之socket-ssh实例
			本文转载自大王http://www.cnblogs.com/alex3714/articles/5830365.html 加有自己的注释,应该会比原文更突出重点些 一. 基本Socket实例 前面讲了 ... 
- Eclipse部署项目的原理简介eclipse,wtpwebapps,tomcat
			转载请注明出处! http://www.cnblogs.com/libingbin/ 感谢您的阅读.如果文章对您有用,那么请轻轻点个赞,以资鼓励. 
