爬塔问题

t2.PNG
def min_time(n,heights):
    """一个位置对应三种状态"""
    states = []
    #初始化
    for i in range(n):
        temp = [0] * 3
        states.append(temp)
    #计算各状态的对应值
    states[0][0] = heights[0]
    states[0][1] = 0
    states[0][2] = 0
    #先跳,后爬
    states[1][0] = heights[1]
    #先爬,后跳
    states[1][1] = heights[0]
    #一次性跳两楼
    states[1][2] = 0
    for i in range(2,n):
        states[i][0] = min(states[i-1])+heights[i]
        states[i][1] = states[i-1][0]
        states[i][2] = states[i-1][1]
    print(states)
    return min(states[n-1])

你可能感兴趣的:(爬塔问题)