Frog Jump

Problem Link: https://www.codingninjas.com/codestudio/problems/frog-jump_3621012?leftPanelTab=1

def frogJump(n: int, heights: List[int]) -> int:

    # dp[i] denotes the minimum energy needed to
    # jump from i to n
    
    dp=[0]*n
    dp[n-1]=0
    dp[n-2]=abs(heights[n-2]-heights[n-1])
    for i in range(n-3,-1,-1):
        firststep=dp[i+1]+abs(heights[i]-heights[i+1])
        secondstep=dp[i+2]+abs(heights[i]-heights[i+2])
        dp[i]=min(firststep,secondstep)
    return dp[0]

Last updated