70 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?

very easy, using recursion:
the state transaction forumla is simple:
climbStairs(n) = climbStairs(n-1) + climbStairs(n-2)

the following code is TLE, because of the stack is too deep and there are many duplicate calculation in this code

class Solution {
    public int climbStairs(int n) {
        if (n == 0) return 0;
        if (n == 1) return 1;
        if (n == 2) return 2;
        return climbStairs(n-1) + climbStairs(n-2);
    }
}

so i use the iterate way:

class Solution {
    
    public int climbStairs(int n) {
        
        int[] memo = new int[n+1];
        memo[1] = 1;
        if (n >= 2) memo[2] = 2;
        for (int i = 3; i <= n; i++) {
            memo[i] = memo[i-1] + memo[i-2];
        }
        return memo[n];
    }
}

746 On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.

dp[n] = Math.min(dp[n-1], dp[n-2])
dp[i] //is the cost that must including ith stair case
dp[i] = Math.min(i-1, i -2) + cost[i];

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int m = cost.length;
        
        
        int[] dp = new int[m];
        dp[0] = cost[0];
        dp[1] = cost[1];
        
        for (int i = 2; i < m; i++) {
            dp[i] = Math.min(dp[i-1], dp[i-2]) + cost[i];
        }
        
        return Math.min(dp[m-1], dp[m-2]);
    }
}