04. Frog Jump
β GFG solution to the Frog Jump problem: find minimum cost for frog to reach last stair by jumping 1 or 2 steps using dynamic programming with space optimization. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an integer array height[] where height[i] represents the height of the i-th stair. A frog starts from the first stair and wants to reach the last stair. From any stair i, the frog has two options:
Jump to the (i+1)th stair
Jump to the (i+2)th stair
The cost of a jump is the absolute difference in height between the two stairs. Your task is to determine the minimum total cost required for the frog to reach the last stair.
π Examples
Example 1
Input: heights[] = [20, 30, 40, 20]
Output: 20
Explanation: Minimum cost is incurred when the frog jumps from stair 0 to 1 then 1 to 3:
jump from stair 0 to 1: cost = |30 - 20| = 10
jump from stair 1 to 3: cost = |20 - 30| = 10
Total Cost = 10 + 10 = 20Example 2
Input: heights[] = [30, 20, 50, 10, 40]
Output: 30
Explanation: Minimum cost will be incurred when frog jumps from stair 0 to 2 then 2 to 4:
jump from stair 0 to 2: cost = |50 - 30| = 20
jump from stair 2 to 4: cost = |40 - 50| = 10
Total Cost = 20 + 10 = 30π Constraints
$1 \le \text{height.size()} \le 10^5$
$0 \le \text{height}[i] \le 10^4$
β
My Approach
The optimal approach uses Dynamic Programming with Space Optimization to efficiently compute the minimum cost:
Space Optimized Dynamic Programming
Identify the Problem Pattern:
This is a classic DP problem where current state depends only on previous two states.
At each stair, we need to make an optimal decision based on cost from previous two stairs.
Define State:
Let
dp[i]represent the minimum cost to reach stairi.We only need to track last two values instead of entire array.
Base Cases:
Cost to reach stair 0 is 0 (starting position).
Cost to reach stair 1 is
|height[1] - height[0]|(only one way to reach).
Recurrence Relation:
For each stair
i, frog can arrive from stairi-1ori-2.dp[i] = min(dp[i-1] + |height[i] - height[i-1]|, dp[i-2] + |height[i] - height[i-2]|)
Space Optimization:
Since we only need previous two states, use two variables
aandbinstead of array.After computing current cost, shift values:
a = b,b = current.
Final Answer:
The minimum cost to reach last stair will be stored in variable
b.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of stairs. We traverse the array once, performing constant time operations at each stair to compute the minimum cost.
Expected Auxiliary Space Complexity: O(1), as we only use two variables to track the previous two states instead of maintaining an entire DP array, resulting in constant space usage.
π§βπ» Code (C)
int minCost(int height[], int n) {
if (n == 1) return 0;
int a = 0, b = abs(height[1] - height[0]);
for (int i = 2; i < n; i++) {
int c = (b + abs(height[i] - height[i - 1]) < a + abs(height[i] - height[i - 2])) ?
b + abs(height[i] - height[i - 1]) : a + abs(height[i] - height[i - 2]);
a = b;
b = c;
}
return b;
}π§βπ» Code (C++)
class Solution {
public:
int minCost(vector<int>& height) {
int n = height.size();
if (n == 1) return 0;
int a = 0, b = abs(height[1] - height[0]);
for (int i = 2; i < n; i++) {
int c = min(b + abs(height[i] - height[i - 1]), a + abs(height[i] - height[i - 2]));
a = b;
b = c;
}
return b;
}
};β Code (Java)
class Solution {
int minCost(int[] height) {
int n = height.length;
if (n == 1) return 0;
int a = 0, b = Math.abs(height[1] - height[0]);
for (int i = 2; i < n; i++) {
int c = Math.min(b + Math.abs(height[i] - height[i - 1]),
a + Math.abs(height[i] - height[i - 2]));
a = b;
b = c;
}
return b;
}
}π Code (Python)
class Solution:
def minCost(self, height):
n = len(height)
if n == 1: return 0
a, b = 0, abs(height[1] - height[0])
for i in range(2, n):
c = min(b + abs(height[i] - height[i - 1]), a + abs(height[i] - height[i - 2]))
a, b = b, c
return bπ§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated