Skip to content

Commit

Permalink
minEnergy
Browse files Browse the repository at this point in the history
  • Loading branch information
ananydev committed Nov 9, 2024
1 parent 0f64d50 commit da65943
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Dynamic Programming/FROM JUMP/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>

int min(int a, int b) {
return (a < b) ? a : b;
}

int minEnergy(int n, int height[]) {
int dp[n]; // Array to store the minimum energy for each stair
dp[0] = 0; // Starting point, so no energy required to be at the first stair

// Fill the dp array from stair 1 to stair n-1
for (int i = 1; i < n; i++) {
int oneStep = dp[i - 1] + abs(height[i] - height[i - 1]);
int twoStep = (i > 1) ? dp[i - 2] + abs(height[i] - height[i - 2]) : oneStep;

dp[i] = min(oneStep, twoStep); // Take the minimum of the two options
}

return dp[n - 1]; // Minimum energy required to reach the last stair
}

int main() {
int n = 4;
int height[] = {10, 20, 30, 10};

int result = minEnergy(n, height);
printf("Minimum energy required: %d\n", result);

return 0;
}

0 comments on commit da65943

Please sign in to comment.