题目:
There is a staircase with N steps and two platforms; one at the beginning, the other at the end of the stairs. On each step a number is written (ranging from -100 to 100 with the exception of 0.) Zeros are written on both platforms. You start going up the stairs from the first platform, to reach the top on the second one. You can move either to the next step or to the next step plus one. You must find the best path to maximize the sum of numbers on the stairs on your way up and return the final sum.
stair-steps
Input: Numbers on each stair as a list of integers.
Output: The final sum for the best way as an integer.
Example:
checkio([5, -3, -1, 2]) == 6
checkio([5, 6, -10, -7, 4]) == 8
checkio([-11, 69, 77, -51, 23, 67, 35, 27, -25, 95]) == 393
checkio([-21, -23, -69, -67, 1, 41, 97, 49, 27]) == 125
链接:
https://py.checkio.org/en/mission/stair-steps/
代码:
def checkio(numbers):
a = numbers
a.insert(0, 0)
a.append(0)
i = 0
x = 0
while i < len(a)-2:
x += a[i]
if (a[i+1] > 0) and (a[i+2] > 0):
i += 1
elif (a[i+2]>= a[i+1]) and (a[i+1] < 0):
i += 2
elif i+4 < len(a):
if (a[i+1] < 0) and (a[i+2] < 0) and ((a[i+2] + a[i+4]) > (a [i+1]+a[i+3])):
i += 2
else:
i += 1
else:
i += 1
if a[-2] > 0:
x += a[-2]
elif a[-2]<0 and i == len(a)-2:
x += a[-2]
return x