Skip to content

Commit

Permalink
Create Geek jump
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored May 13, 2024
1 parent 1495916 commit f6ee1bb
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Geek jump
Original file line number Diff line number Diff line change
@@ -0,0 +1,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];
}
}

0 comments on commit f6ee1bb

Please sign in to comment.