- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Classic DP Problems
Dynamic Programming
Classic DP Problems
Most DP questions are one of four problems wearing different clothes. Learn these and you will start recognising the shape rather than solving from scratch.
1. Climbing stairs - counting ways
You can climb 1 or 2 steps at a time. How many ways to reach step n? The answer for step n is the sum of the ways to reach the two steps you could have come from.
Counting paths
javascript
function climbStairs(n) {
let twoBack = 1
let oneBack = 1
for (let i = 2; i <= n; i++) {
;[twoBack, oneBack] = [oneBack, oneBack + twoBack]
}
return oneBack
}
console.log(climbStairs(5)) // 8