↻one more pass.
Dynamic programming · Easy

Min Cost Climbing Stairs: the idea to remember.

Pay a cost when stepping on a stair and climb one or two steps; start at index zero or one and minimize cost to the top.

Solve on LeetCode ↗

The core insight

Compute each stair's cost plus the cheaper of reaching its previous two stairs.

Why it works

Every path to a stair arrives from exactly one of those previous two positions, giving the optimal substructure.

The mistake to avoid

Reaching the top does not require paying another cost. This implementation updates the cost array.

Time: O(n)Space: O(1)

Commented Java solution

Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.

// Java reference adapted from neetcode-gh/leetcode (MIT).
// Compute each stair's cost plus the cheaper of reaching its previous two stairs.
public int minCostClimbingStairs(int[] cost) {
    int one = 0;
    int two = 0;

    for (int i = cost.length - 1; i >= 0; i--) {
        cost[i] += Math.min(one, two);
        two = one;
        one = cost[i];
    }

    return Math.min(cost[0], cost[1]);
}

Java reference adapted from neetcode-gh/leetcode · MIT license

Make the next connection

Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.