Rosalind第24题:Longest Increasing Subsequence

Problem

subsequence of a permutation is a collection of elements of the permutation in the order that they appear. For example, (5, 3, 4) is a subsequence of (5, 1, 3, 4, 2).

A subsequence is increasing if the elements of the subsequence increase, and decreasing if the elements decrease. For example, given the permutation (8, 2, 1, 6, 5, 7, 4, 3, 9), an increasing subsequence is (2, 6, 7, 9), and a decreasing subsequence is (8, 6, 5, 4, 3). You may verify that these two subsequences are as long as possible.

Given: A positive integer  followed by a permutation  of length .

Return: A longest increasing subsequence of , followed by a longest decreasing subsequence of .

一的排列是按照它们出现的顺序排列的元素的集合。例如,(5、3、4)是(5、1、3、4、2)的子序列。

子序列被增加,如果该子序列增加的元件,并且降低如果元素减少。例如,给定排列(8,2,1,6,5,5,7,4,3,9),增加的子序列是(2,6,7,9,9),减少的子序列是(8,6, 5、4、3)。您可以验证这两个子序列尽可能长。

给定:一个正整数,后跟一个length的排列。

返回值:最长的递增子序列,然后是最长的递减子序列。

 

Sample Dataset

5
5 1 4 2 3

Sample Output

1 2 3
5 4 2

python解决方案: 

def lengthOfLIS(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if nums == []:
        return 0
    N = len(nums)
    Dp = [1] * N
    for i in range(N - 1):
        for j in range(0, i + 1):
            if nums[i + 1] > nums[j]:
                Dp[i + 1] = max(Dp[i + 1], Dp[j] + 1)
    return Dp

nums = [5,1,4,2,3]
dp = lengthOfLIS(nums)
print(dp)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值