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
π 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)
π§βπ» Code (C++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Dynamic Programming (Tabulation)
π‘ Algorithm Steps:
Create a DP array where dp[i] represents minimum cost to reach position i.
Initialize base cases: dp[0] = 0 and dp[1] = cost from 0 to 1.
For each position, compute minimum of jumping from previous or two steps back.
Return the last element containing minimum cost to reach the end.
π Complexity Analysis:
Time: β±οΈ O(n) - Single pass through array
Auxiliary Space: πΎ O(n) - DP array storage
β
Why This Approach?
Clear visualization of subproblem solutions
Easy to debug and trace intermediate values
Foundation for understanding space optimization
π 3οΈβ£ Recursive with Memoization
π‘ Algorithm Steps:
Define recursive function to compute minimum cost from position i to end.
Base case: if at last position, return 0.
Store computed results in memo array to avoid recomputation.
Return minimum of taking one step or two steps forward with respective costs.
π Complexity Analysis:
Time: β±οΈ O(n) - Each state computed once with memoization
Auxiliary Space: πΎ O(n) - Recursion stack and memo array
β
Why This Approach?
Natural top-down thinking process
Easy to convert from pure recursion
Good for understanding DP state transitions
π 4οΈβ£ Bottom-Up with Variable Names
π‘ Algorithm Steps:
Use descriptive variable names for previous two states instead of array indices.
Initialize first two positions as base cases.
Iterate forward computing optimal cost using previous results.
Update variables by shifting: second_prev becomes prev, current becomes new prev.
π Complexity Analysis:
Time: β±οΈ O(n) - Linear iteration through array
Auxiliary Space: πΎ O(1) - Only constant variables used
β
Why This Approach?
Most readable with descriptive variable names
Optimal space complexity
Production-ready clean code
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π·οΈ Space Optimized DP
π’ O(n)
π’ O(1)
π Optimal time & space
π§ Less intuitive initially
π Tabulation DP
π’ O(n)
π‘ O(n)
π Easy to visualize
πΎ Extra space required
π Memoization
π’ O(n)
π‘ O(n)
π― Natural recursion flow
π Stack overhead
π Descriptive Variables
π’ O(n)
π’ O(1)
β Best readability
π§ Slightly more verbose
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π Optimal performance needed
π₯ Space Optimized DP
β β β β β
π Learning DP concepts
π₯ Tabulation DP
β β β β β
π§ Understanding recursion
π₯ Memoization
β β β β β
π― Production code
π Descriptive Variables
β β β β β
β Code (Java)
π Code (Python)
π§ 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