AlgoMaster Logo

Min Cost Climbing Stairs

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Approaches

1. Recursion

Intuition:

Start by identifying that you can reach step i from either step i-1 or i-2. This gives rise to a recursive relation where the cost to reach the i-th step is the cost of the step itself plus the minimum of the costs to reach the two preceding steps.

Code:

2. Top-Down Dynamic Programming (Memoization)

Intuition:

To optimize the recursive solution, we can store the results of subproblems (i.e., costs for each step) and reuse them instead of solving the same subproblems repeatedly.

Code:

3. Bottom-Up Dynamic Programming

Intuition:

Iteratively build up the solution from the base cases (steps 0 and 1) to the target steps, avoiding recursion overhead.

Code:

4. Optimized Bottom-Up Approach

Intuition:

Instead of maintaining an array for dynamic programming, we only need to keep track of the last two costs since the cost to reach each step depends only on the two preceding steps.

Code: