2Brute2Best
🔥0

Min Cost Climbing Stairs

Easy·LeetCode #746 Pro OnlyBasic Reinforcement
LeetCode
01

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.

Input

An array cost, one price per step.

Output

One integer: the least total cost to reach the top.

Example 1
Input: cost = [10, 15, 20]
Output: 15

Begin on step 1 and pay 15, then climb 2 steps to the top. Every other route costs more.

Example 2
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6

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.

Constraints
  • • 2 ≤ cost.length ≤ 1000
  • • 0 ≤ cost[i] ≤ 999
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“minimum total cost”
→ We need the least total, so each step keeps only the cheapest way to reach it.
“climb one or two steps”
→ A step is reached from the one or two steps before it, so the answer for step i uses steps i − 1 and i − 2.
“cost of each step”
→ The price is added when you arrive at a step, so dp[i] = cost[i] + the best of its two arrivals.
03

Approach Complexity Ladder

Brute Force — recursion on the cheapest way to each stepO(2^N)O(N)

Recursive best(i) = cost[i] + min(best(i−1), best(i−2)), recomputed from scratch

Better — memoization (remember best for each step)O(N)O(N)

The same recursion, storing each best(i) the first time it is computed

Optimal — two totals, sliding up the stepsO(N)O(1)

Keep the cheapest totals for the last two steps in two variables and slide them forward

Brute Force Intuition

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:

dp[i] depends on dp[i−1] and dp[i−2]; keep just two variables.
Remember this · DP — 1D Linear
Trigger
"Number of ways", "maximum/minimum up to i", choices at each step that depend on the previous one or two.
Think
If I knew the answer for every smaller prefix, how would I get the answer for i?
Key question
What does dp[i] mean in one sentence?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — 1D Linear translate into production architectures and low-level memory mechanics.