Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FROM JUMP #1833

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Dynamic Programming/FROM JUMP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Minimum Energy to Reach the Last Stair
Problem Description
This program calculates the minimum energy required to reach the last stair in a series of stairs, given the heights of each stair. The energy required to move from one stair to another is calculated based on the height difference between the stairs. At each stair, you can either take a step to the next stair or skip one stair to move two steps ahead, each option consuming different amounts of energy.

Approach
To solve this problem, a dynamic programming (DP) approach is used. The key idea is to minimize the energy required at each stair based on the energy consumption of the previous steps.

Dynamic Programming Array:

We use an array dp[], where dp[i] represents the minimum energy required to reach stair i.
Initialize dp[0] = 0, since no energy is required to start at the first stair.
Energy Calculation:

For each stair i (starting from the second stair), calculate the minimum energy to reach that stair from either:
One step back: dp[i-1] + abs(height[i] - height[i-1])
Two steps back: dp[i-2] + abs(height[i] - height[i-2]) (only if i > 1).
Choose the minimum of these two values to find the optimal (least energy-consuming) path to stair i.
Result:

The last element in the dp array (dp[n-1]) gives the minimum energy required to reach the last stair.
Complexity
Time Complexity: O(n) since we calculate the minimum energy required for each stair once.
Space Complexity: O(n) for the dp array used to store the minimum energy at each stair.
Example
Given height = [10, 20, 30, 10], the function calculates that the minimum energy required to reach the last stair is 20.
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;
}
Loading