Skip to Content
LeetCode The Hard Way
3600 - 3699

3693 - Climbing Stairs II (Medium)

https://leetcode.com/problems/climbing-stairs-ii

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 costs[j]+(j−i)2\text{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

JumpLanding costSquared distanceJump cost
0 → 11121^22
1 → 22121^23
2 → 44222^28

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

JumpLanding costSquared distanceJump cost
0 → 21222^25
2 → 42222^26

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 a total cost of 3+32=123 + 3^2 = 12.

Constraints:

  • 1 <= n == costs.length <= 1e5
  • 1 <= costs[i] <= 1e4

Approach 1: Dynamic Programming

Let dp[i]dp[i] be the minimum total cost to reach step ii. The last jump must come from one of the previous three steps. Each choice pays the same landing cost for step ii, but the squared jump distance adds 11, 44, or 99. Taking the cheapest of these three ways to arrive gives dp[i]=costs[i]+min⁡(dp[i−1]+1,  dp[i−2]+4,  dp[i−3]+9)dp[i] = \text{costs}[i] + \min(dp[i-1] + 1,\; dp[i-2] + 4,\; dp[i-3] + 9).

The loop keeps the previous three costs in aa, bb, and cc, from oldest to newest. For each landing cost xx, it adds xx to the cheapest of the three jump options. The simultaneous assignment shifts the old bb and cc into aa and bb, then stores the new minimum in cc. After the last step, cc holds dp[n]dp[n].

All three variables start at zero. Initially, cc represents step 00, while aa and bb are placeholders. At step 11, the jump from step 00 adds 11, which beats the placeholder options of 44 and 99. At step 22, bb represents step 00, so the direct jump adds 44 and beats the remaining placeholder's 99. From step 33 onward, all three predecessors are valid.

In Example 1, costs=[1,2,3,4]costs = [1, 2, 3, 4] and (a,b,c)(a, b, c) starts at (0,0,0)(0, 0, 0).

StepNew cc using the previous values(a,b,c)(a, b, c) after the update
11+min(1,4,9)=21 + min(1, 4, 9) = 2(0,0,2)(0, 0, 2)
22+min(3,4,9)=52 + min(3, 4, 9) = 5(0,2,5)(0, 2, 5)
33+min(6,6,9)=93 + min(6, 6, 9) = 9(2,5,9)(2, 5, 9)
44+min(10,9,11)=134 + min(10, 9, 11) = 13(5,9,13)(5, 9, 13)

The last jump skips from step 22 to step 44. Reaching step 22 costs 55, the squared distance adds 44, and landing adds another 44, giving the final c=13c = 13.

The time complexity is O(n)O(n) and the space complexity is O(1)O(1).

Written by@wkw
class Solution:
    def climbStairs(self, n: int, costs: List[int]) -> int:
        a = b = c = 0
        for x in costs:
            a, b, c = b, c, x + min(c + 1, b + 4, a + 9)
        return c

On this page