Reach the Staircase Top at Minimum Cost

Quick Overview

Reach the Staircase Top at Minimum Cost. cost[i] is paid when leaving step i, after which you may climb one or two steps.

Reach the Staircase Top at Minimum Cost

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem cost[i] is paid when leaving step i, after which you may climb one or two steps. You may begin at step 0 or step 1. Return the least total cost to reach position cost.length, just past the last step. ```cpp class Solution { public: int minCostClimbingStairs(vector<int>& cost); }; ``` ### Examples **Example 1:** ```text cost = [4,9,3] Output: 7 Explanation: Start at 0, pay 4 to reach 2, then pay 3 to reach the top. ``` **Example 2:** ```text cost = [0,5] Output: 0 Explanation: Start at 0 and jump two positions. ``` ### Constraints - The cost array has 2 through 1000 entries. - Each step cost is between 0 and 999 inclusive. ### Contract considerations Preserve both permitted starting steps and one/two-step moves. ```hint Read the boundary condition What are the only two predecessor positions for a landing? ``` ### Boundary reminders Preserve both permitted starting steps and one/two-step moves.

Overview: Reach the Staircase Top at Minimum Cost. cost[i] is paid when leaving step i, after which you may climb one or two steps.

Google logo
Google
Jul 1, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

cost[i] is paid when leaving step i, after which you may climb one or two steps. You may begin at step 0 or step 1. Return the least total cost to reach position cost.length, just past the last step.

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost);
};

Examples

Example 1:

cost = [4,9,3]
Output: 7
Explanation: Start at 0, pay 4 to reach 2, then pay 3 to reach the top.

Example 2:

cost = [0,5]
Output: 0
Explanation: Start at 0 and jump two positions.

Constraints

  • The cost array has 2 through 1000 entries.
  • Each step cost is between 0 and 999 inclusive.

Contract considerations

Preserve both permitted starting steps and one/two-step moves.

Boundary reminders

Preserve both permitted starting steps and one/two-step moves.