Min Cost Climbing Stairs
Problem Statement
You get an array cost. cost[i] is the price of step i, and you pay that price each time you stand on step i.
From any step you can climb 1 step or 2 steps. You may begin on step 0 or on step 1, and you pay the price of the step you begin on.
Find the least total you must pay to reach the top. The top is one step past the last step (position cost.length). It costs nothing to stand there.
An array cost, one price per step.
One integer: the least total cost to reach the top.
Begin on step 1 and pay 15, then climb 2 steps to the top. Every other route costs more.
Begin on step 0 and pay 1. Then land on steps 2, 4 and 6, each costing 1. Step on to 7 and 9, each costing 1, and finish at the top. Six steps are paid, and each costs 1.
- • 2 ≤ cost.length ≤ 1000
- • 0 ≤ cost[i] ≤ 999
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recursive best(i) = cost[i] + min(best(i−1), best(i−2)), recomputed from scratch
The same recursion, storing each best(i) the first time it is computed
Keep the cheapest totals for the last two steps in two variables and slide them forward
Let best(i) be the least total to stand on step i, price included. To stand on step i you came from step i − 1 or step i − 2, so best(i) = cost[i] + min(best(i − 1), best(i − 2)). The first two steps are the starting points. The answer is the smaller of best(n − 1) and best(n − 2). The method is correct, but it solves the same step many times.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — 1D Linear translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.