8.1 Triple Stair

Dynamic Programming:
For ith stair there are three ways to jump here:
a. From (i-1)th stair and jump one step;
b. From(i-2) th stiar and jump two steps;
c. For(i-3)th stair and jump three steps;

    public static int countWays(int n) {
        // TODO Auto-generated method stub
        if (n<1) return 0;
        int[] dp = new int[n+1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;
        for(int i = 4; i<=n; ++i){
            dp[i] = dp[i-1] + dp[i-2] + dp[i-3];
        }
        return dp[n];
    }
    public static void main(String[] args) {
        int n = 10;
        int ways = countWays(n);
        System.out.println(ways);
    }

你可能感兴趣的:(dp)