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 ...
随机推荐
- iscroll
在原生APP的开发中,有一个常见的功能,就是下拉刷新的功能,这个想必大家都是知道的,但是原生APP的开发,有一个很大的问题就是,你每次更新一些功能,就要用户重新下载一次版本,尤其是在iOS系统中,新版 ...
- “.”(十六进制值 0x00)是无效的字符解决方案
自从我们的项目数据层从读取数据库改为读取接口服务后,经常会出现一些类似于的错误.我们的数据结构如下所示 <type><![CDATA[gp]]></type> &l ...
- Docker的4种网络模式
我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: · host模式,使用--net=host指定. · container ...
- 元首的愤怒 SharePoint Apps
柏林数据中心的服务器机架已经插满.CPU 100%.电力基础设施处在崩溃的边缘,但当元首决定迁移到 Office 365 的时候,将军们却告诉他那里没有 Farm Solution,5 年多的投资将付 ...
- SharePoint 2013 Designer系列之数据视图
在SharePoint使用中,数据展示是一块很重要的部分,很多时候我们会采用webpart的形式,但是有一些情况,我们不必使用开发,仅需使用Designer即可,下面让我简单介绍下数据视图的使用. 1 ...
- jQuery修改class属性和CSS样式
jQuery修改class属性和CSS样式 class属性修改 类属性即class属性,规定类名. 用类选择器规定样式的时候,需要为元素指定类名,即class属性的值. 注意每个HTML元素只有一个c ...
- CSS3-06 样式 5
浮动(Float) 关于浮动,要说的可能就是:一个设置了浮动的元素会尽量向左移动或向右移动,且会对其后的元素造成影响,其后的元素会排列在其围绕在其左下或右下部.似乎就这么简单,但是在实际开发中,它应用 ...
- yii2分页的基本使用及其配置详解
作者:白狼 出处:http://www.manks.top/yii2_linkpager_pagination.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置 ...
- jQuery对表格的操作及其他应用
表格操作 1.隔行变色:对普通表格进行隔行换色:单击显示高亮样式:复选框选中高亮 <!DOCTYPE html> <html> <head> <meta ht ...
- 深入理解Linux修改hostname
当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...