Core Concepts

graph TD A["Fib(4)"] --> B["Fib(3)"] A --> C["Fib(2)"] B --> D["Fib(2)"] B --> E["Fib(1)"] C -.-> |"Cached!"| F["O(1)"]

Cheatsheet Formulas

Classic Problem: Climbing Stairs

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?

class Solution {
    public int climbStairs(int n) {
        if (n <= 2) return n;
        
        int first = 1;
        int second = 2;
        
        for (int i = 3; i <= n; i++) {
            int third = first + second;
            first = second;
            second = third;
        }
        
        return second;
    }
}
class Solution:
    def climbStairs(self, n: int) -> int:
        if n <= 2:
            return n
            
        first, second = 1, 2
        
        for _ in range(3, n + 1):
            first, second = second, first + second
            
        return second