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 ...
随机推荐
- Java Class.cast方法
1.Java api public T cast(Object obj); Casts an object to the class or interface represented 解释的比较笼统, ...
- 纯CSS打造好看的按钮样式
好看的按钮.链接.div样式,效果预览: http://hovertree.com/code/run/css/s8o19792.html 发现今天积分和排名不错: 代码如下: <!DOCTYPE ...
- php文件之间传值的三种主流并且常用的方式
一.表单传值 在<form>中的action填入要跳转页面的路径,method填入POST或者GET方法.表单中的提交按钮按下后,就会把<form>中有value都传到要跳转的 ...
- Android Touch事件传递机制 一: OnTouch,OnItemClick(监听器),dispatchTouchEvent(伪生命周期)
ViewGroup View Activity dispatchTouchEvent 有 有 有 onInterceptTouchEvent 有 无 无 onTouchEvent 有 有 有 例 ...
- Android Studio关于SVN的相关配置及从SVN检出项目
一.安装配置: 如图,安装时必须自定义选择 command line 否则不会安装的 安装完成后,打开 IDE 的 setting 配置面板: 如上图路径 Version Control 下的 Sub ...
- Android自定义控件7--自定义开关--绘制界面内容
本文实现全自定义控件--自定义开关 本文地址:http://www.cnblogs.com/wuyudong/p/5922316.html,转载请注明源地址. 自定义开关 (View),本文完成下面内 ...
- android 自定义view中findViewById为空的解决办法
网上说的都是在super(context, attrs);构造函数这里少加了一个字段, 其实根本不只这一个原因,属于view生命周期的应该知道,如果你在 自定义view的构造函数里面调用findVie ...
- android studio 导入项目太慢(卡条)
原因: 下载的项目和你当前已经下载的grandle 不一致, 导致导入的时候到网上下载相应的版本. 最简单的办法,修改你这个需要导入的项目. 需要修改的文件: 1. xxx-project/.idea ...
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- Servlet、Filter、Listener、Interceptor
首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角 色,Interceptor是某些MVC框架中的角色,比如Str ...