Frog Jump Problem Statement
A frog is positioned on the first step of a staircase consisting of N
steps. The goal is for the frog to reach the final step, i.e., the N
th step. The height of each step is provided in the array HEIGHT
. If the frog jumps from the i
th step to the j
th step, the energy expended during the jump is represented by the absolute difference |HEIGHT[i-1] - HEIGHT[j-1]|
. From the i
th step, the frog can jump to either the (i+1)
th step or the (i+2)
th step. The task is to determine the minimum total energy expenditure required for the frog to travel from the first step to the N
th step.
Input:
The first line of input contains an integer 'T,' indicating the number of test cases.
Each test case starts with an integer 'N,' the number of stairs in the staircase.
The following line comprises the 'HEIGHT' array, specifying the heights of each stair step.
Output:
For each test case, return a single integer denoting the minimal energy required to reach the last stair.
Example:
Input:
HEIGHT = [10,20,30,10]
Output:
20
Explanation:
The frog can jump from the 1st stair to the 2nd stair, incurring an energy cost of |20-10| = 10. Subsequently, it jumps from the 2nd stair to the last stair, at an energy cost of |10-20| = 10. The total energy expended is thus 20.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 100000
1 ≤ HEIGHT[i] ≤ 1000
- Time limit: 1 sec
Top CGI Group Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in CGI Group Associate Software Engineer
Reviews
Interviews
Salaries
Users/Month