-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1495916
commit f6ee1bb
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |