最长递增子序列详解(longest increasing subsequence)

数据结构与算法最长递增子序列 阅读详情

一个各公司都喜欢拿来做面试笔试题的经典动态规划问题,互联网上也有很多文章对该问题进行讨论,但是我觉得对该问题的最关键的地方,这些讨论似乎都解释的不很清楚,让人心中不快,所以自己想彻底的搞一搞这个问题,希望能够将这个问题的细节之处都能够说清楚。

对于动态规划问题,往往存在递推解决方法,这个问题也不例外。要求长度为i的序列的Ai{a1,a2,……,ai}最长递增子序列,需要先求出序列Ai-1{a1,a2,……,ai-1}中以各元素(a1,a2,……,ai-1)作为最大元素的最长递增序列,然后把所有这些递增序列与ai比较,如果某个长度为m序列的末尾元素aj(j<i)比ai要小,则将元素ai加入这个递增子序列,得到一个新的长度为m+1的新序列,否则其长度不变,将处理后的所有i个序列的长度进行比较,其中最长的序列就是所求的最长递增子序列。举例说明,对于序列A{35, 36, 39, 3, 15, 27, 6, 42}当处理到第九个元素(27)时,以35, 36, 39, 3, 15, 27, 6为最末元素的最长递增序列分别为
    35
    35,36
    35,36,39
    3
    3,15
    3,15,27
    3,6
当新加入第10个元素42时,这些序列变为
    35,42
    35,36,42
    35,36,39,42,
    3,42
    3,15,42
    3,15,27,42
    3,6,42

这其中最长的递增序列为(35,36,39,42)和(3,15,27,42),所以序列A的最长递增子序列的长度为4,同时在A中长度为4的递增子序列不止一个。

该算法的思想十分简单,如果要得出Ai序列的最长递增子序列,就需要计算出Ai-1的所有元素作为最大元素的最长递增序列,依次递推Ai-2,Ai-3,……,将此过程倒过来,即可得到递推算法,依次推出A1,A2,……,直到推出Ai为止,

代码如下
unsigned int LISS(const int array[], size_t length, int result[])
{
    unsigned int i, j, k, max;

    //变长数组参数,C99新特性,用于记录当前各元素作为最大元素的最长递增序列长度
    unsigned int liss[length];

    //前驱元素数组,记录当前以该元素作为最大元素的递增序列中该元素的前驱节点,用于打印序列用
    unsigned int pre[length];

    for(i = 0; i < length; ++i)
    {
        liss[i] = 1;
        pre[i] = i;
    }

    
最长递增子序列(LIS)的三种算法 最长递增子序列:给定一个长度为N的数组,找出一个最长的单调递增子序列子序列不一定连续,但初始顺序不能乱。 比如数组A={1,3,4,2,5},其最长递增子序列为1,3,4,5 方法一:最长公共子序列法 对于给定长度为N的数组A: 使数组B为排序后的数组A (O(NlogN)) 求出A与B的最长公共子序列(LCS) (O(N2)) 对求得的公共子序列进行去重 (O(N)... 阅读详情

相关推荐

最长递减子序列

/** * 求一个数组的最长递减子序列 *输入:9,4,3,2,5,4,3,2 *输出:9,5,4,3,2 **/ #include #include #include using namespace std; struct state { int index_pre; //存储当前值的前一个值的位置 int length; //存储到当前位置的长度 state() :i

leewon1988的专栏 784

最长递增子序列问题(你真的会了吗)

1.对应牛客网链接2.题目描述: 3.解题思路下面以[5,7,1,9,4,6,2,8,3]为例:4.对应代码:1.对应牛客网链接:2.题目描述: 3.解题思路:4.对应代码:1.对应letecode链接:2.题目描述:3.解题思路4.对应代码:...

qq_56999918的博客 9623

算法系列——最长递增子序列(Longest Increasing Subsequence)

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the leng

BridgeGeorge 1488

最长递增子序列

问题 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱)。例如:给定一个长度为6的数组A{5, 6, 7, 1, 2, 8},则其最长的单调递增子序列为{5,6,7,8},长度为4. 解法1:最长公共子序列法 这个问题可以转换为最长公共子序列问题。如例子中的数组A{5,6, 7, 1, 2, 8},则我们排序该数组得到数组A‘{1, 2, 5, 6, 7,...

切梦 5404

最长递增子序列Longest Increasing Subsequence

定义 最长上升子序列Longest Increasing Subsequence,LIS),在计算机科学上是指一个序列中最长的单调递增子序列。 问题描述 给定一个长度为 N 的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱)。例如:给定一个长度为 5 的数组{5, 6, 1, 2, 8},则其最长的单调递增子序列为 {5,6,8},长度为 3。 解法 动态规划 时间复杂度 该方法的时间复杂度为 。 实现过程 下面我们用一个实例来分析一下动态规划求解 LIS 的整个过程。

努力中的老周的专栏 4364

算法: 最长递增子序列300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Given an integer array nums, return the length of the longest strictly increasing subsequence. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of t

AI架构师易筋 4411

LeetCode - 673. Number of Longest Increasing Subsequence(最长递增子序列的个数)

LeetCode - 673. Number of Longest Increasing Subsequence(最长递增子序列的个数) 题目链接 题目 解析 做这题之前先要知道求一个数组的最长递增子序列。 做法: 求出最长递增子序列的长度(max),可以用记忆化也可以递推; 然后遍历数组,看以哪些数结尾的序列是最长序列,然后对每一个这样的序列进行递归处理,从后往前求以这个结尾的最长序列的个数...

zxzxin的博客~ 365

最长递增子序列 (Longest Increasing Subsequence)

问题描述: 给定一个序列 An = a1,a2,  ... , an,找出最长子序列使得对所有 i j,ai aj。显然,暴力算法的时间复杂度是 O(2n),因为搜索空间呈指数级增长。对于这种问题,如果要找复杂度为多项式时间的算法,自然而然地会想到动态规划。首先,要找出一种方法

夜鱼的专栏 1万+

最长递增子序列-Longest Increasing Subsequence

动态规划的经典题! package DP; import java.util.Arrays; public class LIS { public static void main(String[] args) { int[] A = {5,3,4,8,6,7}; System.out.println(lis(A)); System.out.println(lis

1827

[LeetCode] Longest Increasing Subsequence 最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given[10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is[2...

weixin_34162401的博客 129

最长递增子序列(Longest Increasing Subsequence)-C语言实现

最长递增子序列 动态规划 C语言

Jasonchen1224的博客 2683

最长递增子序列longest increasing subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Have you met this question in a real interview? Example For [5, 4, 1, 2, ...

就是这个七昂的博客 574

最长递增子序列(Longest increasing subsequence)

问题定义: 给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续)。 这道题共3种解法。 1. 动态规划 动态规划的核心是状态的定义和状态转移方程。定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度。可以得到以下的状态转移方程: d(i) = max(1, d(j) + 1), 其中j < i,且A[j] <=...

weixin_33817333的博客 282

C/C++ longest increasing subsequence 最长递增子序列算法详解及源码

算法的基本思想是遍历给定序列中的每个元素,记录以该元素结尾的最长递增子序列的长度。通过比较当前元素与前面的元素,可以确定以当前元素结尾的最长递增子序列的长度。在遍历过程中,保持一个数组dp,其中dp[i]表示以第i个元素结尾的最长递增子序列的长度。最长递增子序列Longest Increasing Subsequence算法是一种动态规划算法,用于找到一个给定序列中最长的严格递增子序列。这个子序列可以不连续,但元素的顺序必须保持递增

希望我的博客,能帮上你解决学习中工作中所遇到的问题 2895

【小白笔记】最长递增子序列Longest Increasing Subsequence, 简称 LIS)

结尾的最长 LIS 长度,而是维护一个特殊的数组。时,我们可以向前看所有。这是解决 LIS 问题的。

weixin_70325224的博客 698

python 实现longest increasing subsequence最长递增子序列算法

输出为4,表示最长递增子序列为[10, 22, 33, 60]。

luthane的博客 650

动态规划之最长递增子序列Longest Increasing Subsequence

原文地址:http://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/ We have discussed Overlapping Subproblems and Optimal Substructure properties in Set 1 and Set 2respect

K.Sun 4215
下一篇: 扔鸡蛋问题详解(Egg Dropping Puzzle)
joylnwang
博客等级 码龄18年 436粉丝 19原创
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值