1-D Dynamic Programing List of common 1-D dynamic programing.
- Source:
Example
import { climbStairs } from '../../1d-dp';
console.log(climbStairs(2)); // 2
console.log(climbStairs(3)); // 3
console.log(climbStairs(6)); // 13
Methods
(inner) climbStairs(n) → {number}
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
For example,
Input: n = 5
Output: 8
Time Complexity for constructor: O(n)
Space Complexity for constructor: O(n)
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
n |
number
|
number of steps to reach the top |
Returns:
- Type:
-
number
Number of ways one can climb the stairs
(inner) minCostClimbingStairs(cost) → {number}
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.
For example,
Input: cost = [10,15,20]
Output: 15
Time Complexity for constructor: O(n)
Space Complexity for constructor: O(n)
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
cost |
Array.<number>
|
All cost to reach the top |
Returns:
- Type:
-
number
Minimum cost required to reach the top