求子串累加的最大值
# 求子串累加的最大值
p = [1, -2, -2, 3,10,-4,7,2,-5]
n = len(p)
l = [0] * n
for i in range(n):
mino = 0
j = i-1
while j>=0:
if p[j] < 0:
mino += p[j]
j -= 1
else:
break
e = p[i] + mino
if i > 0:
if e >= 0:
l[i] = l[i-1] + e
else:
l[i] = max(l[i-1], p[i])
else:
if p[i] > 0:
l[i] = p[i]
####################################################################################
# 求子串累加的最大值
p = [1,-5,1, 2,-2, 3,10,-4,7,2,-5]
n = len(p)
l = [0] * n
for i in range(n):
l[i] = max(l[i-1]+p[i], p[i])
最大上升子串
# 最大上升子串
p = [1, -2, -2, 3, 10, 4, 7]
p2 = [1, -2, -1, 3, 10, -4, 7, 2, -5]
p3 = [1, 4, 3, 2, 6, 7]
def getdp2(arr):
n = len(arr)
dp = [0] * n
dp[0] = 1
l = [1]
if n > 1:
for i in range(1, n):
ma = 0
for j in range(i):
if arr[i] > arr[j] and dp[j] + 1 > ma:
ma = dp[j] + 1
else:
ma = 1
dp[i] = ma
print(dp)
l.append(max(dp))
print(l)
return dp
dp = getdp2(p3)
##################################################################################
# 最大上升子串
p = [7, 1, 4, -2, 3, 6, 8, 10, 4, 7]
n = len(p)
l = [0] * n
l[0] = 1
x = []
for i in range(n):
for j in range(i):
# print(j, i)
if p[j] < p[i]:
l[i] = l[j] + 1
else:
l[i] = 1
print(l, j, i)
x.append(max(l))
print(x)
啥玩意啊
#!/usr/bin/env python
#-*- coding:utf-8 -*-
p = [1,4,3,2,6,5]
arr = [1,4,5,6,3,4,5,6,7]
# n = len(p)
# l2 = [0] * n
# def getdp1(arr):
# for i in range(n):
# if i > 0:
# for j in range(i):
# if p[j] < p[i]:
# l2[i] = max(1+l2[j], l2[i])
# else:
# l2[i] = 1
# def getdp1(arr):
# n = len(arr)
# dp = [0] * n
# from ipdb import set_trace
# set_trace()
# for i in range(n):
# dp[i] = 1
# for j in range(i):
# if arr[i] > arr[j]:
# dp[i] = max(dp[i], dp[j] + 1)
def getdp1(arr):
n = len(arr)
dp = [0] * n
for i in range(n):
dp[i] = 1
for j in range(i):
if arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
return dp
c = getdp1(arr)