Recently I gave an interview for Zepto and they asked a questions related to climbing stairs. I was not able to solve it. Can anyone help me with an approach for this? I know that this is related to DP but I am not able to make a logic.
Let's say there are total 'n' floors in a buliding. The a person needs to go from 2nd Floor to 6th Floor. In order to move up they can take 'U' steps (0<U<=n) upwards , to move down they can 'D' steps (0<D<=n) downwards. You can assume for taking every U/D step cost is 1. You need to calculate the minimum cost to reach a floor. If you cannot reach the destination floor return -1.
Example 1: Total number of builidings(n) = 50 Source = 2nd Floor, Destination = 6th floor, U=2, D=2
One way
=> 2-> 4 ->6(Destination) -> Cost =2 (taking 2 upward steps only)
Second way
=> 2 -> 4 ->6 -> 8 -> 6(Destination) -> Cost = 4(taking 3 upwards step of 2 leaps and 1 downward steps of 1 leap)
Example 2: Total number of builidings(n) = 50 Source = 2nd Floor, Destination = 6th floor, U=5, D=5
There is no way to reach the 6th floor, so ans = -1