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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks for the minimum cost to reach the top of the stairs, where you can climb one or two steps at a time. The brute force approach involves exploring every possible path you can take to reach the top. We check the cost of each path and choose the one with the lowest total cost.
Here's how the algorithm would work step-by-step:
def min_cost_climbing_stairs_brute_force(cost):
def calculate_min_cost(current_index):
# If we reach the top (or beyond), return 0 as no further cost is incurred.
if current_index >= len(cost):
return 0
# Explore taking one step.
one_step_cost = cost[current_index] + calculate_min_cost(current_index + 1)
# Explore taking two steps.
two_step_cost = cost[current_index] + calculate_min_cost(current_index + 2)
# Choose the path with the minimum cost.
return min(one_step_cost, two_step_cost)
# Consider starting from the first and second step.
start_from_first_step = calculate_min_cost(0)
start_from_second_step = calculate_min_cost(1)
# Return the overall minimum cost.
return min(start_from_first_step, start_from_second_step)
The most efficient way to find the minimum cost to reach the top is to work backward from the top. We figure out the cheapest way to reach each step, one by one, until we know the cheapest way to reach the starting steps.
Here's how the algorithm would work step-by-step:
def min_cost_climbing_stairs(cost):
number_of_steps = len(cost)
minimum_cost = [0] * number_of_steps
#The cost to reach the first two steps is defined.
minimum_cost[0] = cost[0]
minimum_cost[1] = cost[1]
for i in range(2, number_of_steps):
#Calculate the minimum cost to reach each step.
minimum_cost[i] = cost[i] + min(minimum_cost[i - 1], minimum_cost[i - 2])
#The top can be reached from the last or second to last step.
return min(minimum_cost[number_of_steps - 1], minimum_cost[number_of_steps - 2])
Case | How to Handle |
---|---|
Null or empty cost array | Return 0 since there are no costs to consider and we are effectively at the top. |
Cost array with one element | Return that single element's cost, as we can directly reach the top from either index 0 or 1. |
Cost array with two elements | Return the minimum of the two elements, as we choose the cheaper starting point. |
Large cost values potentially leading to integer overflow | Use a data type with a larger range (e.g., long) to store the minimum costs to avoid overflow. |
Cost array with all zero values | The algorithm should still correctly compute the minimum cost as 0, choosing the path with no cost. |
Cost array with all very large values | The algorithm should still correctly compute the minimum cost, although it may be large, within the allowed data type range. |
Very large input array size | The dynamic programming approach has O(n) space and time complexity, which scales linearly with the input size and should handle large arrays efficiently. |
Negative cost values (if problem states costs are non-negative) | If negative costs are invalid, throw an exception or return an error code; otherwise, the algorithm should handle them correctly as it searches for the minimum cost path. |