checkio Stair Steps

题目:
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值