-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geek jump
46 lines (35 loc) · 1.04 KB
/
Geek jump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Geek jump
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
public static void main(String args[]) throws IOException{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- > 0) {
int N = sc.nextInt();
int[] arr = new int[N];
for(int i = 0; i < N; i++)
arr[i] = sc.nextInt();
Solution obj = new Solution();
int res = obj.minimumEnergy(arr, N);
System.out.println(res);
}
}
}
class Solution{
public int minimumEnergy(int arr[],int n){
if(n == 1) {
return 0;
}
int[] dp = new int[n];
dp[0] = 0;
dp[1] = Math.abs(arr[1] - arr[0]);
for(int i = 2; i < n; i++) {
int jumpOne = dp[i - 1] + Math.abs(arr[i] - arr[i - 1]);
int jumpTwo = dp[i - 2] + Math.abs(arr[i] - arr[i - 2]);
dp[i] = Math.min(jumpOne, jumpTwo);
}
return dp[n - 1];
}
}