Leetcode: Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

This is a classic Dynamic Programming problem.

Define:

f(n)= number of ways you can climb to the nth step.

To reach to the nth step, you have only two choices:

  1. Advance one step from the n-1th step.
  2. Advance two steps from the n-2th step.

Therefore, f(n) = f(n-1) + f(n-2), which is the exact same recurrence formula defined by theFibonacci sequence (with different base cases, though).

Set base cases f(1) = 1f(2) = 2 and you are almost done.

Now, we could calculate f(n) easily by storing previous values in an one dimension array and work our way up to n. Heck, we can even optimize this further by storing just the previous two values.

int climbStairs(int n){int p =1, q =1;for(int i =2; i <= n; i++){int temp = q;
        q += p;
        p = temp;}return q;}

Interestingly, this problem could also be solved using combinatorics, as shown in the code by@saw2010.

For example, let's assume n = 6.

Let:

x = number of 1's,
y = number of 2's.

We could reach the top using one of the four combinations below:

+=======+| x | y |+===+===+|6|0|=>1)Six single steps.|4|1|=>2)Four single steps and one double step.|2|2|=>3)Two single steps and two double steps.|0|3|=>4)Threedouble steps.+===+===+

For the first combination pair (x,y) = (6,0), there's obviously only one way of arranging six single steps.

For the second combination pair (4,1), there's five ways of arranging (think of it as slotting the double step between the single steps).

Similarly, there are six ways C(4,2) and one way C(3,3) of arranging the third and fourth combination pairs respectively.

Generally, for pair(x, y), there are a total of C(x+y,y) = (x+y)! / (x!*y!) ways of arranging the 1's and 2's.

The total number of possible ways is the sum of all individual terms,

f(6)=1+5+6+1=13.

Generalizing for all n's (including odd n),

f(n)= C(n,0)+ C(n-1,1)+ C(n-2,2)+...+ C(ceiling(n/2), floor(n/2))


 1 class Solution {
 2 public:
 3     int climbStairs(int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (n == 0 || n == 1)
 7             return 1;
 8         vector< int > s(n, 0);
 9         s[0] = 1;
10         s[1] = 1;
11         for (int i = 2; i < s.size(); i++)
12             s[i] = s[i-1] + s[i-2];
13         return s[s.size() - 1] + s[s.size() - 2];
14     }
15 };

 


 

posted @ 2013-05-02 12:42  caijinlong  阅读(142)  评论(0编辑  收藏  举报