300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
长度为N的数组记为A={a0 a1 a2...an-1};
记A的前i个字符构成的前缀串为Ai= a0 a 1a2...ai-1,以ai结尾的最长递增
子序列记做Li,其长度记为a[i];
假定已经计算得到了a[0,1…,i-1],如何计算a[i]呢?
根据定义, Li必须以ai结尾,如果将ai缀到L0 L1…… Li-1的后面,是否
允许呢?
如果aj<ai,则可以将ai缀到Lj的后面,并且使得Lj的长度变长。
从而:a[i]={max(a(j))+1, 0 ≤j≤i-1且a[j]≤a[i] }
需要遍历在i之前的所有位置j,找出满足条件a[j]≤a[i]的a[j];
计算得到a[0…n-1]后,遍历所有的a[i],找出最大值即为最大递增子序列
的长度。
时间复杂度为O(N2)。
思考:如何求最大递增子序列本身?
记录前驱
class Solution {
public int lengthOfLIS(int[] a) {
if(a.length==0) return 0;
int[] longs = new int[a.length];
for(int i = 0;i<a.length;i++)
longs[i] = 1;
int max = longs[0];
for(int i = 1;i < a.length;i++){
for(int j = 0;j <= i-1;j++)
if(a[j]<a[i])
if(longs[i]<longs[j]+1)
longs[i] = longs[j]+1;
//如果求序列本身,在这里记录前驱
if(max<longs[i])
max = longs[i];
}
return max;
}
class Solution {
public:
int lengthOfLIS(vector<int>& a) {
if (a.size()==) return ;
int max = ;
vector<int> dp(a.size(),);
for (int i = ;i < a.size(); i++) {
for (int j = ; j < i ;j++) {
if (a[j] < a[i]) {
dp[i] = std::max(dp[j]+,dp[i]);
}
}
max = std::max(dp[i],max);
}
return max;
}
};
class Solution(object):
def lengthOfLIS(self, a):
n = len(a)
if n ==0:
return 0
dp = [1] * n
for i in range(n):
for j in range(i):
if(a[i]>a[j] and dp[i]<dp[j]+1):
dp[i] = dp[j]+1 #dp[i]现在存储的即为以a[i]结尾最长递增子序列长度
#求dp数组中最大者即为最长的长度 return max(dp)
300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)的更多相关文章
- LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- 65.Longest Increasing Subsequence(最长增长子序列)
Level: Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...
- POJ2533 Longest Ordered Subsequence 【最长递增子序列】
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32192 Acc ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
随机推荐
- LAMP环境搭建博客
背景: 公司要用到lamp环境,让我装,我就开始着手找资料,一般分为源码装和yum装,源码装很容易出错,所以我选择了yum装,. 服务器:aliyun服务器 centos6.8系统 按照第一个安装完 ...
- 在Sql2000 sql2005 sql2008 下已能实现事务复制的强制订阅,但请求订阅始终不能实现总有下列错误提示
硬件环境 : 一台服务器 安装了 sqlserver2008 数据库 局域网还有一台机器 安装了 sqlserver2000数据库 两台server 通信 共享均没有问题 同步过程中遇到的问题 : ...
- Java各种日期格式的获取和设置指定日期
因为近期在做一个项目,发现项目中日期设置的bug,于是查阅了多方资料后.最终攻克了,为此写篇总结.方便日后的查阅. 多的不说了.直接上代码 package com.example.testdate; ...
- Hadoop单机安装配置过程:
1. 首先安装JDK,必须是sun公司的jdk,最好1.6版本以上. 最后java –version 查看成功与否. 注意配置/etc/profile文件,在其后面加上下面几句: export JAV ...
- M451例程讲解之按键
/**************************************************************************//** * @file main.c * @ve ...
- 【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP
[BZOJ4155][Ipsc2015]Humble Captains Description 每天下午放学时都有n个zky冲出教室去搞基.搞基的zky们分成两队,编号为1的zky是1号队的首领,编号 ...
- Vue基础-渲染函数-父子组件-传递数据
Vue 测试版本:Vue.js v2.5.13 做了个 demo,把父子组件的数据都绑定到 Vue 实例 app 上,注释中的 template 相对好理解些 <div id="app ...
- 03.Curator深入使用
1.Apache Curator简介 Curator提供了一套Java类库,可以更容易的使用ZooKeeper.ZooKeeper本身提供了Java Client的访问类,但是API太底层,不 ...
- ipad4没有声音提示消息
打开『设置』-『通用』-侧边开关用于: 1:锁定屏幕旋转 2:静音 √ 把对号去掉 选择1即可
- Spring 拦截器的使用
一.Web.xml配置 在Web.xml 配置Spring核心控制器DispatcherServlet接收所有请求 <servlet> <servlet-name>spring ...