This is what the problem you linked to says:
A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
This is completely different from what you described in your post. The following solves the actual topcoder problem.
dp[i, 0] = maximum length subsequence ending at i such that the difference between the
last two elements is positive
dp[i, 1] = same, but difference between the last two is negative
for i = 0 to n do
dp[i, 0] = dp[i, 1] = 1
for j = 0 to to i - 1 do
if a[i] - a[j] > 0
dp[i, 0] = max(dp[j, 1] + 1, dp[i, 0])
else if a[i] - a[j] < 0
dp[i, 1] = max(dp[j, 0] + 1, dp[i, 1])
Example:
i = 0 1 2 3 4 5 6 7 8 9
a = 1 17 5 10 13 15 10 5 16 8
dp[i, 0] = 1 2 2 4 4 4 4 2 6 6
dp[i, 1] = 1 1 3 3 3 3 5 5 3 7
^ ^ ^ ^
| | | -- gives us the sequence {1, 17, 5, 10}
| | -- dp[2, 1] = dp[1, 0] + 1 because 5 - 17 < 0.
| ---- dp[1, 0] = max(dp[0, 1] + 1, 1) = 2 because 17 - 1 > 0
1 element
nothing to do
the subsequence giving 7 is 1, 17, 5, 10, 5, 16, 8, hope I didn't make any careless
mistakes in computing the other values)
Then just take the max of both dp
arrays.