Mr. Monkey is in a garden, and stands in front of a row of banana trees. Let the trees be numbered as t1, t2, · · · tn as usual. He will start walking from some point, say s which is to the left of the leftmost tree. From this point, assume that he moves only to his right. Once he reaches a tree, he has two choices - either to walk past it or climb it and eat exactly one banana. Eating a banana make him jump a some distance over a few trees. In case he lands on the ground, he can continue the above exercise from that point. In case the first jump lands him on a tree, he can either climb down and continue the exercise or he can eat the bananas from there and continue jumping.
Let dist be an array that contains the distance of ti from the starting point s and jump be an array that contains the distance that Mr. Monkey can jump if he eats a banana from ti
Write an O(n*2) algorithm that determines the maximum distance that Mr. Monkey can jump. Note that this does not include the distance he walks or climbs.
Comments
Leave a comment