Skip to main content

Climbing Stairs II - Solution & Explanation

MediumArrayDynamic Programming9 min read
Practice this problem

Problem Statement

You are climbing a staircase with n + 1 steps, numbered from 0 to n.

You are also given a 1-indexed integer array costs of length n, where costs[i] is the cost of step i.

From step i, you can jump only to step i + 1, i + 2, or i + 3. The cost of jumping from step i to step j is defined as: costs[j] + (j - i)2

You start from step 0 with cost = 0.

Return the minimum total cost to reach step n.

 

Example 1:

Input: n = 4, costs = [1,2,3,4]

Output: 13

Explanation:

One optimal path is 0 → 1 → 2 → 4

Jump Cost Calculation Cost
0 → 1 costs[1] + (1 - 0)2 = 1 + 1 2
1 → 2 costs[2] + (2 - 1)2 = 2 + 1 3
2 → 4 costs[4] + (4 - 2)2 = 4 + 4 8

Thus, the minimum total cost is 2 + 3 + 8 = 13

Example 2:

Input: n = 4, costs = [5,1,6,2]

Output: 11

Explanation:

One optimal path is 0 → 2 → 4

Jump Cost Calculation Cost
0 → 2 costs[2] + (2 - 0)2 = 1 + 4 5
2 → 4 costs[4] + (4 - 2)2 = 2 + 4 6

Thus, the minimum total cost is 5 + 6 = 11

Example 3:

Input: n = 3, costs = [9,8,3]

Output: 12

Explanation:

The optimal path is 0 → 3 with total cost = costs[3] + (3 - 0)2 = 3 + 9 = 12

 

Constraints:

  • 1 <= n == costs.length <= 105โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹
  • 1 <= costs[i] <= 104

Approach Overview

Problem Overview: You are given a staircase where each index represents a step and the value at that index indicates how many steps you can climb forward from there. The goal is to compute how many distinct ways you can reach the final step starting from the beginning.

Approach 1: Recursive Exploration (Exponential Time)

The most direct idea is to try every possible jump from the current step. From index i, iterate through all reachable steps from i + 1 up to i + nums[i]. Recursively compute the number of ways to reach the top from each of those positions and sum the results. This approach mirrors the decision tree of the problem but recomputes the same subproblems many times. Time complexity grows exponentially O(k^n) in the worst case, with O(n) recursion stack space.

Approach 2: Dynamic Programming with Memoization (O(n * k) time, O(n) space)

Dynamic Programming removes repeated work by caching results for each step. Define dp[i] as the number of ways to reach the end starting from step i. When computing dp[i], iterate over all reachable next steps and sum their values: dp[i] += dp[j] for j in the range (i+1 ... i+nums[i]). Memoization ensures each state is solved once. The total work becomes proportional to the number of states multiplied by the maximum jump length, giving O(n * k) time and O(n) space.

Approach 3: Bottom-Up Dynamic Programming (O(n * k) time, O(n) space)

The same recurrence can be computed iteratively. Start from the last step, where there is exactly one way to finish. Move backward through the array and compute dp[i] by summing values of reachable future states. Each step performs a bounded loop over its jump range, making the total complexity O(n * k). This approach avoids recursion overhead and is usually preferred in production implementations. It relies heavily on the idea that every state depends only on states ahead of it.

Both optimized solutions rely on recognizing overlapping subproblems, which is the core signal for Dynamic Programming. The iteration over the input also highlights its relationship with Array traversal problems and typical state transition patterns.

Recommended for interviews: Bottom-up Dynamic Programming. Interviewers expect you to first recognize the recursive structure and then convert it into a DP relation. Mentioning the brute force recursion shows you understand the search space, but implementing the O(n * k) DP solution demonstrates control over state design and optimization.

Solution

We define f[i] as the minimum total cost required to reach the i-th stair, initially f[0] = 0, and all other f[i] = +infty.

For each stair i, we can jump from the (i-1)-th, (i-2)-th, or (i-3)-th stair, so we have the following state transition equation:

$ f[i] = min_{j=i-3}^{i-1} (f[j] + costs[i - 1] + (i - j)^2)

Where costs[i] is the cost of the i-th stair, and (i - j)^2 is the jump cost from the j-th stair to the i-th stair. Note that we need to ensure j is not less than 0.

The final answer is f[n].

The time complexity is O(n) and the space complexity is O(n), where n$ is the number of stairs.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor โ†’

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive Brute ForceO(k^n)O(n)Conceptual understanding of the search space or very small inputs
DP with MemoizationO(n * k)O(n)Top-down reasoning when recursion feels more natural
Bottom-Up Dynamic ProgrammingO(n * k)O(n)Preferred production or interview solution due to iterative efficiency

Video Solution

Climbing Stairs II | LeetCode 3693 | Standard DSA Problem โ€ข Sanyam IIT Guwahati โ€ข 961 views views

Watch 7 more video solutions โ†’

Frequently Asked Questions

Is Climbing Stairs II easy or hard?
Climbing Stairs II is usually categorized as a Medium problem. The recurrence relation is straightforward once you recognize the dynamic programming pattern, but handling the jump ranges efficiently requires careful iteration or optimization.
Climbing Stairs II Python/Java solution
Both Python and Java implementations typically use a DP array and iterate from the last index toward the beginning. For each step i, sum dp values for all reachable next steps within the allowed jump range. The logic is identical across Python, Java, C++, Go, TypeScript, and Rust.
How to solve Climbing Stairs II in O(n)?
An O(n) improvement is possible by maintaining a running window sum of reachable dp states instead of recomputing each range repeatedly. As you move backward, update the window to include new reachable states and remove ones that fall outside the jump range. This converts repeated summations into constant-time updates per index.
What is the best approach for Climbing Stairs II?
Dynamic Programming with a bottom-up table is the most practical solution. Define dp[i] as the number of ways to reach the top from step i and compute values from the end of the array backward. Each state aggregates the ways of all reachable next steps. This runs in O(n * k) time with O(n) extra space.
Is Climbing Stairs II asked at Google/Amazon/Meta?
Variations of staircase counting and jump-based dynamic programming problems appear frequently in interviews at companies like Google, Amazon, and Meta. The pattern tests your ability to identify overlapping subproblems and design a correct DP state transition.
What data structure is used in Climbing Stairs II?
The core data structure is a one-dimensional DP array where each index stores the number of ways to reach the top from that step. The input itself is typically represented as an array that defines how far you can jump from each position.
What is the time complexity of Climbing Stairs II?
The optimized dynamic programming solution runs in O(n * k) time, where n is the number of steps and k is the maximum jump length from any step. Each step may iterate through up to k reachable next steps. Space complexity is O(n) for the DP array.

Ready to solve this problem?

Practice Climbing Stairs II with our built-in code editor and test cases.

Practice on FleetCode