您的位置:首页 > 大数据 > 人工智能

Leetcode: Climbing Stairs

2013-05-02 12:42 218 查看
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
n
th step, you have only two choices:

Advance one step from the
n-1
th step.

Advance two steps from the
n-2
th 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) = 1
,
f(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))


[/code]

class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n == 0 || n == 1)
return 1;
vector< int > s(n, 0);
s[0] = 1;
s[1] = 1;
for (int i = 2; i < s.size(); i++)
s[i] = s[i-1] + s[i-2];
return s[s.size() - 1] + s[s.size() - 2];
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: