code every day with me

–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…


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » code every day with me." duccanhole | Sciencx - Tuesday November 2, 2021, https://www.scien.cx/2021/11/02/code-every-day-with-me-2/
HARVARD
duccanhole | Sciencx Tuesday November 2, 2021 » code every day with me., viewed ,<https://www.scien.cx/2021/11/02/code-every-day-with-me-2/>
VANCOUVER
duccanhole | Sciencx - » code every day with me. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/02/code-every-day-with-me-2/
CHICAGO
" » code every day with me." duccanhole | Sciencx - Accessed . https://www.scien.cx/2021/11/02/code-every-day-with-me-2/
IEEE
" » code every day with me." duccanhole | Sciencx [Online]. Available: https://www.scien.cx/2021/11/02/code-every-day-with-me-2/. [Accessed: ]
rf:citation
» code every day with me | duccanhole | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.