zoukankan      html  css  js  c++  java
  • 最长的递减或递增序列-python

    要求求出给出的序列中的最长的递增或递减序列。

    笨办法-两个动态规划合并

    input = [5, 2, 3, 4, 1]
    
    def longList(nums):
        dp1 = [1] * len(nums)  # 递减dp
        dp2 = [1] * len(nums)
        result1 = 1
        result2 = 1
    
        for i in range(len(nums)-1):
            if nums[i]>nums[i+1]:
                dp1[i+1] = dp1[i]+1
            else:
                dp2[i+1] = dp2[i]+1
            result1 = max(result1, dp1[i + 1])
            result2 = max(result2, dp2[i + 1])
        return max(result1, result2)
    res = longList(input)
    print(res)
  • 相关阅读:
    iptables
    apt
    cvc-elt.1: Cannot find the declaration of element 'beans'.
    di
    log
    java内存模型
    spring-jms
    JTS
    10java进阶——IO2
    17单例
  • 原文地址:https://www.cnblogs.com/harbin-ho/p/15368453.html
Copyright © 2011-2022 走看看