Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Atharv2433 authored Oct 23, 2024
1 parent 8ca926b commit 60f26b6
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Frogenergy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package DP;
import java.util.Scanner;
public class Frogenergy {



public int fun(int n , int arr[],int dp[]){
if(n == 0){
return 0;
}
if(n == 1){
return Math.abs(arr[0] - arr[1]);
}
if(dp[n] != -1){
return dp[n];
}
int one = fun(n-1,arr,dp) + Math.abs(arr[n] - arr[n-1]);
int two = fun(n-2,arr,dp) + Math.abs(arr[n] - arr[n-2]);

return dp[n] = Math.min(one,two);
}
public static void main(String args[]){

Scanner sc = new Scanner(System.in);
Frogenergy f = new Frogenergy();

int n = sc.nextInt();
int arr[] = new int[n];
int dp[] = new int[n];

for(int i = 0; i < n; i++){
dp[i] = -1;
}

for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}

int res = f.fun(n-1,arr,dp);

System.out.println(res);;
}
}

0 comments on commit 60f26b6

Please sign in to comment.