Tuesday, June 7, 2011

WAP to Find Number of Way You Can Climb The Stairscase



"You are climbing a staircase. Each time you can either take one step or two. The staircase has n steps. In how many distinct ways can you climb the staircase?"

Algorithm & Approach
is I got the problem correctly then its really nice recursive problem that can solved using F(n)=F(n-1)+F(n-2) recurrence solution as its given that we can either can goto 1 step or 2 step so its just like calculating Fibonacci number using recursion which has exponentiation time complexity . we can initialize f(0)=0 & f(1)=1 as 1st pretty clear that when we are at ground floor we don't need any step to climb & to climb on next staircase up we need only 1 step from 0th staircase. so we start in from n=2 calculate nth Fibonacci number will gives us number of distinct ways can you climb the staircase.

To Calculate The nth Fibonacci Number All Possible & Optimized Solution I have posted in This Post

http://shashank7s.blogspot.com/2011/03/wap-fro-fibonacci-numbers.html

Data Structure Used: Array is Sufficient

Time Complexity O(logn)Most Efficient
Space Complexity O(n)

3 comments :

Notting_Hill said...

isnt calculatingg nth fibonacci linear in time?

Unknown said...

@Notting_Hill I Posted the Most Efficient algo to calculate the nth Fibonacci number , u can have a look & let me know if anything wrong. :)

Notting_Hill said...

yup i know abt the O(logn) algorithm...thanks for making the correction