You are given an integer array
You can either start from the step with index
Return the minimum cost to reach the top of the floor.
Output: 15
Explanation: We can start at index 1 and pay 15, then climb two steps.
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.
So, the total cost is 6.
We will be taking this highlighted indexes path. [
Contents
Solution using brute force approach Solution using DP memoization Solution using DP Bottom-Up approach using tabulation Solution using DP Bottom-Up approach memory optimized
In this approach, we will compute all possible outcomes recursively, first by stating at index 0 and take minimum cost to reach top of the floor, then start at index 1 and take minimum cost to reach top of the floor. Finally take minimum between these two outcomes.
-
We will create a function
bruteForceApproach that takescost array as input argument. -
Create another helper function
bruteForceApproachRec that takecost andstart variable to indicate the start index of the sub array we should be considering.- Call this function recursively, by moving start to +1 and +2 indexes as the problem states, we can either takes 1 step or steps. Finally take the minimum of these two possibilities, add it to current node and return to its parent node.
- Base case will be, when it reaches end of array or goes out of bounds as we are trying with +1 and +2 index from every start index.
- Call the above created helper function from index 0 and 1 as the problem states, we can either start at index 0 or index 1, and take the minimum values from these results.
Complexity Analysis:
Time complexity: Above code runs in O(2n) time where
Space complexity: O(n) as we are making recursive loops
From the above brute force approach, we can notice that there are some overlapping problems.
For example, let's say
- If we draw a decision tree with possible paths, it looks like below diagram.
- If you notice above diagram, highlighted paths are overlapping sub problems.
-
So, in this approach, we will be caching sub problem result into a
HashMap withstart index as key and its result as value. When a sub problem is repeated and found in cache, we will return it, otherwise we will the function recursively and cache its result.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the cache we are maintaining and stack for recursive calls.
In this approach we will look at solving the problem using Dynamic programming using tabulation caching mechanism.
The time and memory complexity will be same as above solution using memoization,
but we can notice a pattern to improve memory complexity to constant in later solution.
Intution behind this solution is, when we are at
Base cases for this solution will be
Finally, take the minimum stored at last two indexes of the
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) since we have a
If we look at above DP solution using tabulation, we can see that we just need previous two values only.
If we are at step
-
In this approach, we will create two variables
prev andcurrent to hold previous values for i-2nd step and i-1st steps respectively. -
Once we calculate minimum cost possible at current indexm we will assign that to
current and copy previous current toprev for next iteration.
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1) since we are only using prev, current and a temp variables.
Above implementations source code can be found at