My Blog List

[Leetcode Solution] Climbing Stairs

Analysis 

  • Dynamic programming
  • f[i] = f[i-1] + f[i-2]

Note

Code


class Solution {
public:
int climbStairs(int n) {
vector<int>f(n+1,0);
f[0]=0;
f[1]=1;
f[2]=2;
for(int i=3;i<=n;i++)
f[i]=f[i-1]+f[i-2];
return f[n];
}
};
UPDATED AT LEETCODE 2ND PASS
class Solution {
public:
int climbStairs(int n) {
vector<int>f(n+1,1);
for(int i=2;i<=n;i++)
f[i]=f[i-1]+f[i-2];
return f[n];
}
};

No comments:

Post a Comment

Enter you comment