The Riddle source : https://www.youtube.com/watch?v=7yDmGnA8Hw0

Here are the constraints :

1. N people are standing at one side of bridge at night

2. At the max only 2 people can cross the bridge with a lantern

3. At the max only 2 people can be illuminated by the lantern

4. A person stays at, either the start of the bridge or end of the bridge without lantern

5. Without lantern, a person/two persons can not cross the bridge

6. If more time than the minimum time is taken, then zombies will eat the person at the start of the bridge or try to cross the bridge when a person/2 persons crossing the bridge, thereby collapsing the bridge

I tried the below approach. Please help me to make it better :

Let, CrossBridge(n) = Time taken to make n people stand at the other side of bridge

Pi = Time taken by Person i to cross bridge one way

CrossBridge(1) = P1

CrossBridge(2) = max(P1,P2)

CrossBridge(3) = min ( CrossBridge(2) + P1 + max(P3.P1),

CrossBridge(2) + P2 + max(P3,P2) )

CrossBridge(4) = min ( CrossBridge(3) + P1 + max(P4,P1),

CrossBridge(3) + P2 + max(P4,P2),

CrossBridge(3) + P3 + max(P4,P3) )

CrossBridge(5) = min ( CrossBridge(4) + P1 + max(P5,P1),

CrossBridge(4) + P2 + max(P5,P2),

CrossBridge(4) + P3 + max(P5,P3),

CrossBridge(4) + P4 + max(P5,P4) )

For Ex., CrossBridge(3) = Two people have already crossed, lets say P1 and P2. Either P1 must take P3 to other side by first travelling to start of bridge or P2 must take P3 to other side by travelling to the start of bridge and which ever is minimum!

CrossBridge(n) = min( CrossBridge(n-1) + Pi + max(Pn,Pi) ) where 1<= i <= n-1

Updates :

1. Lets assume the time taken by 4 people are : 1 mt, 2mt, 5mt and 10 mt and we can use the above recurrence for all the permutations of the array.

2 I had the chance to code the problem :

http://code.geeksforgeeks.org/QoYlbt

but this is giving wrong answer for the the input set mentioned in the youtube which is [1,2,5,10]

The solution is 17 minutes but my code outputs 19 minutes