My solution using permutation and combination


  • 0
    M

    This is my solution using permutation and combination
    We can take a look of the problem: if it need i two-steps up to the top, there are n-i steps include two-step and one-step totally. So we can select i from n-i. That is permutation and combination.

    int climbStairs(int n) {
    if (n == 0 || n == 1)
        return n;
    int sum = 1;
    for (int i = 1; i * 2 <= n; i++){
        double sum2 = (n-i);
        int count = (n-i);
        int count2 = i;
        double sum3 = i;
        for (int j = 1; j < i; j++){
            sum2 *= (--count);
            sum3 *= (--count2);
        }
        sum += (sum2/sum3);
    }
    
    return sum;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.