Problem p03160 (python)

Fixed submission:
import math
n = int(input())
h = list(map(int,input().split(' ')))
'''
n-=1
inf = int(1e9+5)

dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
'''
def solve(n,h):
    dp = [0]*n
    for i in reversed(range(0,n-1)):
        if i==n-1:
            dp[i] = 0
        else:
            if i==n-2:
                dp[i] = abs(h[n-1]-h[n-2])
                continue
            a = 0
            b = 0
            if i+1<=n-1:
                a = dp[i+1]+abs(h[i]-h[i+1])
            if i+2<=n-1:
                b = dp[i+2]+abs(h[i]-h[i+2])  
            dp[i] = min(a,b)
    return dp[0]
print(solve(n,h))

Buggy submission (verdict: Runtime Error):
import math
from functools import lru_cache
def Frog1():
    n = int(input())
    h = list(map(int,input().split(' ')))
    @lru_cache(None)
    def Solve(i):
        if i==n-1:
            return 0
        if i==n-2:
            return Solve(i+1)+abs(h[i]-h[i+1])
        return min(Solve(i+1)+abs(h[i]-h[i+1]),Solve(i+2)+abs(h[i]-h[i+2]))
    return Solve(0)

print(Frog1())

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
h = list(map(int,input().split(' ')))
'''
n-=1
inf = int(1e9+5)

dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
'''
def solve(n,h):
    dp = [0]*n
    for i in reversed(range(0,n-1)):
        if i==n-1:
            dp[i] = 0
        else:
            a = 0
            b = 0
            if i+1>n-1:
                a = math.inf
            else:
                a = dp[i+1]+abs(h[i]-h[i+1])
            if i+2>n-1:
                b = math.inf
            else:
                b = dp[i+2]+abs(h[i]-h[i+2])
            dp[i] = min(a,b)
    return dp[0]
print(solve(n,h))

Buggy submission (verdict: Runtime Error):

n = int(input())
h = list(map(int,input().split(' ')))

n-=1
inf = int(1e9+5)

dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))

Buggy submission (verdict: Runtime Error):

n = int(input())
h = list(map(int,input().split(' ')))

n-=1
inf = int(1e9+5)

dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))

Buggy submission (verdict: Runtime Error):
import math
n = int(input())
h = list(map(int,input().split(' ')))

n-=1
dp = {}
def frog1(i):
    if i==n:
        return 0
    if i in dp:
        return dp[i]
    
    a = 0
    b = 0
    if i+1>n:
        a = math.inf
    else:
        a = frog1(i+1)+abs(h[i]-h[i+1])

    if i+2>n:
        b = math.inf
    else:
        b = frog1(i+2)+abs(h[i]-h[i+2])
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split(' ')))

dp = {}
myinf = int(1e9 + 5)

def frog1(i):
    if i==n-1:
        return 0
    if i>n-1:
        return myinf
    
    if i in dp:
        return dp[i]
    
    a = frog1(i+1)
    b = frog1(i+2)
    h1 = myinf if i+1>n-1 else h[i+1]
    h2 = myinf if i+2>n-1 else h[i+2]
    
    a = a + abs(h[i]-h1)
    b = b + abs(h[i]-h2)
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split(' ')))

dp = {}
myinf = int(1e9)

def frog1(i):
    if i==n-1:
        return 0
    if i>n-1:
        return myinf
    
    if i in dp:
        return dp[i]
    
    a = frog1(i+1)
    b = frog1(i+2)
    h1 = myinf if i+1>n-1 else h[i+1]
    h2 = myinf if i+2>n-1 else h[i+2]
    
    a = a + abs(h[i]-h1)
    b = b + abs(h[i]-h2)
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split(' ')))

dp = {}
myinf = int(1e9)

def frog1(i):
    if i>=n:
        return 0
    if i in dp:
        return dp[i]
    
    a = frog1(i+1)
    b = frog1(i+2)
    h1 = 0 if i+1>n-1 else h[i+1]
    h2 = 0 if i+2>n-1 else h[i+2]
    a = a + abs(h[i]-h1)
    b = b + abs(h[i]-h2)
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0)-h[len(h)-1])

Buggy submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split(' ')))

dp = {}

def frog1(i):
    if i>=n-1:
        return 0
    if i in dp:
        return dp[i]
    
    a = frog1(i+1)
    b = frog1(i+2)
    h1 = 0 if i+1>n-1 else h[i+1]
    h2 = 0 if i+2>n-1 else h[i+2]
    a = a + abs(h[i]-h1)
    b = b + abs(h[i]-h2)
    dp[i] = min(a,b)
    return dp[i]
print(frog1(0))