suppose there is a ladder having snake at some distance.we can only take 1,2,3 steps at a time to climb to the ladder find the number of steps
Sir how to approach this?
you mean minimum number of steps, right?
- if there is only one snake, you can solve this problem in constant time by forming some mathematical expressions like if the snake is not in the distance of multiple of three, then the ans is around (total distance)/3 and involve other conditions.
- I am assuming snakes at multiple distances, the solution is linear in time.
you can solve it recursively as:
say f(x) is min no of steps to cover the distance x.
then
f(x) = 1+ min{ f(x-1) if no snake at 1
f(x-2) if no snake at 2
f(x-3) if no snake at 3
}
you should try writing code on yourself.
thanks
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.