Given a series x(i), i from 1 to N, let’s say N = 10,000.
for any i < j,
D(i,j) = x(i) - x(j), if x(i) > x (j); or,
= 0, if x(i) <= x(j).
Define
Dmax(im, jm) := max D(i,j), for all 1 <= i < j <=N.
What’s the best algorithm to calculate Dmax, im, and jm?
I tried to use Dynamic programming, but this seems is not dividable… Then i’m a bit lost… Could you guys please suggest? is backtracking the way out?
Iterate over the series, keeping track of the following values:
For each element, there are two possible values for the new maximum descent:
maximumElementSoFar - newElementSo pick the one which gives the higher value. The maximum descent at the end of iteration will be your result. This will work in linear time and constant additional space.