要求求出给出的序列中的最长的递增或递减序列。
笨办法-两个动态规划合并
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)