Problem p03160 (python)

Fixed submission:
N = input() 
heights = input()

N = int(N)
heights = list(map(int, heights.split(' ')))

def solve_problem_dp(N, heights):
    if N == 1:
        return 0
    elif N == 2:
        return abs(heights[0] - heights[1])
    costs = [0] * len(heights)
    costs[1] = abs(heights[0] - heights[1])

    for i in range(2, len(heights)):
        cost_jump1 = abs(heights[i] - heights[i-1]) + costs[i-1]
        cost_jump2 = abs(heights[i] - heights[i-2]) + costs[i-2] 
        best_cost = min(cost_jump1, cost_jump2)
        costs[i] = best_cost
    
    return costs[-1]


print(solve_problem_dp(N, heights))

Buggy submission (verdict: Runtime Error):
N = input() 
heights = input()

N = int(N)
heights = map(int, heights.split(' '))

def solve_problem(N, heights):
    memory = {}

    def minimum_cost(N, heights):
        mem_key = str(N) + str(heights)
        if mem_key in memory: return memory[mem_key]
        if N == 1:
            return 0
        elif N == 2:
            return abs(heights[0] - heights[1])

        jump_one_cost = abs(heights[0] - heights[1]) + minimum_cost(N-1, heights[1:])
        jump_two_cost = abs(heights[0] - heights[2]) + minimum_cost(N-2, heights[2:])

        best_cost = min(jump_one_cost, jump_two_cost)
        memory[mem_key] = best_cost
        return best_cost
    
    return minimum_cost(N, heights)

print(solve_problem(N, heights))

Buggy submission (verdict: Runtime Error):
x = input()
y = input()
z = input()

Buggy submission (verdict: Runtime Error):
N = input() 
heights = input()

N = int(N)
heights = map(int, heights.split(' '))

def solve_problem(N, heights):
    memory = {}

    def minimum_cost(N, heights):
        mem_key = str(N) + str(heights)
        if mem_key in memory: return memory[mem_key]
        if N == 1:
            return 0
        elif N == 2:
            return abs(heights[0] - heights[1])

        jump_one_cost = abs(heights[0] - heights[1]) + minimum_cost(N-1, heights[1:])
        jump_two_cost = abs(heights[0] - heights[2]) + minimum_cost(N-2, heights[2:])

        best_cost = min(jump_one_cost, jump_two_cost)
        memory[mem_key] = best_cost
        return best_cost
    
    return minimum_cost(N, heights)

print(solve_problem(test3_N, test3_heights))

Buggy submission (verdict: Runtime Error):
N, heights = input().split('\n')

N = int(N)
heights = map(int, heights.split(' '))

Buggy submission (verdict: Runtime Error):
N, heights = input().split('\n')

N = int(N)
heights = map(int, heights.split(' '))

def solve_problem(N, heights):
    memory = {}

    def minimum_cost(N, heights):
        mem_key = str(N) + str(heights)
        if mem_key in memory: return memory[mem_key]
        if N == 1:
            return 0
        elif N == 2:
            return abs(heights[0] - heights[1])

        jump_one_cost = abs(heights[0] - heights[1]) + minimum_cost(N-1, heights[1:])
        jump_two_cost = abs(heights[0] - heights[2]) + minimum_cost(N-2, heights[2:])

        best_cost = min(jump_one_cost, jump_two_cost)
        memory[mem_key] = best_cost
        return best_cost
    
    return minimum_cost(N, heights)

print(solve_problem(N, heights))

Buggy submission (verdict: Runtime Error):
N, heights = input().split('\n')

N = int(N)
heights = map(int, heights.split(' '))

def solve_problem(N, heights):
    memory = {}

    def minimum_cost(N, heights):
        mem_key = str(N) + str(heights)
        if mem_key in memory: return memory[mem_key]
        if N == 1:
            return 0
        elif N == 2:
            return abs(heights[0] - heights[1])

        jump_one_cost = abs(heights[0] - heights[1]) + minimum_cost(N-1, heights[1:])
        jump_two_cost = abs(heights[0] - heights[2]) + minimum_cost(N-2, heights[2:])

        best_cost = min(jump_one_cost, jump_two_cost)
        memory[mem_key] = best_cost
        return best_cost
    
    return minimum_cost(N, heights)

print(solve_problem(test3_N, test3_heights))