This content originally appeared on DEV Community and was authored by duccanhole
--DAY 13--
Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:
- Problem: Climbing Stairs
- Detail: here
- Idea: use recursion
- There are two way to climb to n'th stair: climb 1 step from n'th-1 stair or climb 2 steps from n'th-2 stair
- Similar to Fibonacci
- My solution (javascript): With recursion
var climbStairs = function(n) {
if(n==1||n==2) return n;
return climbStairs(n-1)+climbStairs(n-2);
};
No recursion
var climbStairs = function(n) {
if(n==1||n==2) return n;
let dp=[];
dp[0]=1; dp[1]=2;
for(let i=2;i<n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n-1];
};
-->If you have better solution or any question, please comment below. I will appreciate.
This content originally appeared on DEV Community and was authored by duccanhole
duccanhole | Sciencx (2021-11-02T02:46:02+00:00) code every day with me. Retrieved from https://www.scien.cx/2021/11/02/code-every-day-with-me-2/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.