Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20] Output: 15 Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1] Output: 6 Explanation: You will start at index 0. - Pay 1 and climb two steps to reach index 2. - Pay 1 and climb two steps to reach index 4. - Pay 1 and climb two steps to reach index 6. - Pay 1 and climb one step to reach index 7. - Pay 1 and climb two steps to reach index 9. - Pay 1 and climb one step to reach the top. The total cost is 6.
Constraints:
2 <= cost.length <= 10000 <= cost[i] <= 999Problem summary: You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1. Return the minimum cost to reach the top of the floor.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[10,15,20]
[1,100,1,1,1,100,1,1,100,1]
climbing-stairs)find-number-of-ways-to-reach-the-k-th-stair)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #746: Min Cost Climbing Stairs
class Solution {
private Integer[] f;
private int[] cost;
public int minCostClimbingStairs(int[] cost) {
this.cost = cost;
f = new Integer[cost.length];
return Math.min(dfs(0), dfs(1));
}
private int dfs(int i) {
if (i >= cost.length) {
return 0;
}
if (f[i] == null) {
f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2));
}
return f[i];
}
}
// Accepted solution for LeetCode #746: Min Cost Climbing Stairs
func minCostClimbingStairs(cost []int) int {
n := len(cost)
f := make([]int, n)
for i := range f {
f[i] = -1
}
var dfs func(int) int
dfs = func(i int) int {
if i >= n {
return 0
}
if f[i] < 0 {
f[i] = cost[i] + min(dfs(i+1), dfs(i+2))
}
return f[i]
}
return min(dfs(0), dfs(1))
}
# Accepted solution for LeetCode #746: Min Cost Climbing Stairs
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
@cache
def dfs(i: int) -> int:
if i >= len(cost):
return 0
return cost[i] + min(dfs(i + 1), dfs(i + 2))
return min(dfs(0), dfs(1))
// Accepted solution for LeetCode #746: Min Cost Climbing Stairs
impl Solution {
pub fn min_cost_climbing_stairs(cost: Vec<i32>) -> i32 {
let n = cost.len();
let mut f = vec![-1; n];
fn dfs(i: usize, cost: &Vec<i32>, f: &mut Vec<i32>, n: usize) -> i32 {
if i >= n {
return 0;
}
if f[i] < 0 {
let next1 = dfs(i + 1, cost, f, n);
let next2 = dfs(i + 2, cost, f, n);
f[i] = cost[i] + next1.min(next2);
}
f[i]
}
dfs(0, &cost, &mut f, n).min(dfs(1, &cost, &mut f, n))
}
}
// Accepted solution for LeetCode #746: Min Cost Climbing Stairs
function minCostClimbingStairs(cost: number[]): number {
const n = cost.length;
const f: number[] = Array(n).fill(-1);
const dfs = (i: number): number => {
if (i >= n) {
return 0;
}
if (f[i] < 0) {
f[i] = cost[i] + Math.min(dfs(i + 1), dfs(i + 2));
}
return f[i];
};
return Math.min(dfs(0), dfs(1));
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.