I've been studying dynamic programming lately and came across a problem that is, I believe (might be wrong, though), a modification of the classic longest alternating subsequence (LAS) problem.
But instead of asking you what is the maximum length, this problem asks what is the minimum number of changes you would have to do to any
V[i] element such that the whole input array becomes an alternating sequence. Changes can only be subtractions, not additions.
2 5 8 6 1 2
In this case the minimum number is
1: you can make the
4, for example.
The answer could be
0 as well if the whole sequence is already alternating.
For solving the LAS, I know we need to retain in
DP[i] the size of the longest alternating subsequence ending at
i for which the last element is greater than the previous one and in
DP[i] the same when the last element is lesser than its previous. And we build this by iterating from 1 to
But how can I find the changes I'd need to make to turn the whole sequence into an alternating one?