From f6ee1bba8cab622ac498e750ca9913f2c6bcb975 Mon Sep 17 00:00:00 2001 From: Disha Thakurata <146114938+dishathakurata@users.noreply.github.com> Date: Tue, 14 May 2024 02:04:38 +0530 Subject: [PATCH] Create Geek jump --- Geek jump | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Geek jump diff --git a/Geek jump b/Geek jump new file mode 100644 index 0000000..6aeae65 --- /dev/null +++ b/Geek jump @@ -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]; + } +}