Algorithm


Problem Name: 746. Min Cost Climbing Stairs

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

 

Example 1:

Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.

Example 2:

Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.

 

Constraints:

  • 2 <= cost.length <= 1000
  • 0 <= cost[i] <= 999

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

Copy The Code & Try With Live Editor

Input

x
+
cmd
cost = [10,15,20]

Output

x
+
cmd
15

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int minCostClimbingStairs(int[] cost) {
    int n = cost.length;
    int first = cost[0];
    int second = cost[1];
    for (int i = 2; i  <  n; i++) {
      int curr = cost[i] + Math.min(first, second);
      first = second;
      second = curr;
    }
    return Math.min(first, second);
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
cost = [10,15,20]

Output

x
+
cmd
15

#3 Code Example with Javascript Programming

Code - Javascript Programming


const minCostClimbingStairs = function(cost) {
  let f1 = cost[0];
  let f2 = cost[1];
  for (let i = 2; i  <  cost.length; i++) {
    let f_cur = cost[i] + Math.min(f1, f2);
    f1 = f2;
    f2 = f_cur;
  }
  return Math.min(f1, f2);
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
cost = [1,100,1,1,1,100,1,1,100,1]

Output

x
+
cmd
6

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def minCostClimbingStairs(self, cost):
        """
        :type cost: List[int]
        :rtype: int
        """
        for i in range(2,len(cost)): cost[i]+=min(cost[i-1],cost[i-2])
        return min(cost[-1],cost[-2])
Copy The Code & Try With Live Editor

Input

x
+
cmd
cost = [1,100,1,1,1,100,1,1,100,1]

Output

x
+
cmd
6

#5 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0746_MinCostClimbingStairs
    {
        public int MinCostClimbingStairs(int[] cost)
        {
            int cost1 = 0, cost2 = 0;
            var index = 2;
            for (; index  <  cost.Length; index++)
            {
                var curr = Math.Min(cost1 + cost[index - 2], cost2 + cost[index - 1]);
                cost1 = cost2;
                cost2 = curr;
            }

            return Math.Min(cost1 + cost[index - 2], cost2 + cost[index - 1]);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
cost = [10,15,20]

Output

x
+
cmd
15
Advertisements

Demonstration


Previous
#745 Leetcode Prefix and Suffix Search Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#747 Leetcode Largest Number At Least Twice of Others Solution in C, C++, Java, JavaScript, Python, C# Leetcode