Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
Be sure that you clarify the input and output parameters of the problem:
O(n)
time and O(1)
space will do. Run through a set of example cases:
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.
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.
EDGE CASE
Input: cost = []
Output: 0
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Bottom-Up DP Technique, we will build up our answer.
1. Manage edge case of 2 steps and under
2. Reuse the cost array to build up to our answer
3. From the given numbers we can build up to any number of number of steps.
a. The minimum cost of step is the result of the current step and minimum between two previous steps
4. Return the minimum cost of climbing stairs by getting the minimum cost of last two steps
⚠️ Common Mistakes
O(n!)
. So use DP to bring down the cost to O(n)
Implement the code to solve the algorithm.
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
# Manage edge case of 2 steps and under
if len(cost) <= 2:
return min(cost)
# Reuse the cost array to build up to our answer
for i in range(2, len(cost)):
# From the given numbers we can build up to any number of number of steps
# The minimum cost of step is the result of the current step and minimum between two previous steps
cost[i] = cost[i] + min(cost[i-1], cost[i-2])
# Return the minimum cost of climbing stairs by getting the minimum cost of last two steps
return min(cost[i], cost[i-1])
class Solution {
public int minCostClimbingStairs(int[] cost) {
// Reuse the cost array to build up to our answer
int n = cost.length ;
for(int i=2 ; i<n ; i++){
// From the given numbers we can build up to any number of number of steps
// The minimum cost of step is the result of the current step and minimum between two previous steps
cost[i] = Math.min(cost[i-1] , cost[i-2]) + cost[i];
}
// Return the minimum cost of climbing stairs by getting the minimum cost of last two steps
return Math.min(cost[n-1] , cost[n-2]);
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the array, which also represents the number of steps and it's cost.
O(N)
, because we need to build up to the N
stepsO(1)
, because for the Bottom-Up approach we reuse the given array.