Dynamic Programming 04-the longest zigzag sequence

Source: Internet
Author: User

Topcode Links:
https://community.topcoder.com/statc=problem_statement&pm=1259&rd=4493
Problem:

A sequence ofNumbers isCalled a zig-zag sequenceif  theDifferencesbetweenSuccessive numbers strictly alternatebetweenPositive andNegative. The FirstDifference (ifOne exists) may be either positiveorNegative. A sequence withFewer than, elements isTrivially a zig-zag sequence. For example,1,7,4,9,2,5  isA zig-zag sequence because theDifferences (6,-3,5,-7,3) is alternately positive andNegative. In contrast,1,4,7,2,5  and 1,7,4,5,5Is notZig-zag sequences, the  FirstBecause its  FirstDifferences is positive and  the SecondBecause its  LastDifference isZero. Given a sequence ofintegers, sequence,return  the length  of  theLongest subsequence ofSequence that  isA zig-zag sequence. A subsequence isObtained bydeletingsome  Number  ofElements (possibly zero) from  theOriginal sequence, leaving theRemaining elementsinchTheir original order.

Examples
0)
{1, 7, 4, 9, 2, 5}
Returns:6
The entire sequence is a zig-zag sequence.
1)
{1, 17, 5, 10, 13, 15, 10, 5, 16, 8}
Returns:7
There is several subsequences that achieve this length. One is 1,17,10,13,10,16,8.
2)
{44}
Returns:1
3)
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Returns:2
4)
{70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32}
Returns:8
5)
{374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
249, 22, 176, 279, 23, 22, 617, 462, 459, 244}
Returns:36

The answer to the question is the same as the LIS, but to consider the difference between positive and negative alternating, so we need a state to save at this time is positive or negative.
(1) Status
D[i] for arrays to Array[i], the longest zigzag length
State[i] to indicate array[i] compared to the last number is large or small, than the previous number is 1, small is 0, note that the number is not continuous, that is, the index of the previous number is not necessarily i-1, but to meet the zigzag sequence the longest time of the previous number.

Here's a straightforward workaround:

intLongestzigzag ( vector<int>&nums) {intN=nums.size (); vector<int>States (N,0); vector<int>RETs (N,1); for(intI=1; i!=n;i++) { for(intj=0; j!=i;j++) {if((states[j]==0)|| (states[j]==-1&AMP;&AMP;NUMS[I]&GT;NUMS[J]) | | (states[j]==1&AMP;&AMP;NUMS[I]&LT;NUMS[J]))if(rets[j]+1>rets[i]) {rets[i]=rets[j]+1;if(Nums[i]>nums[j]) states[i]=1;if(Nums[i]<nums[j]) states[i]=-1; }//std::cout<<i<< ":" <<rets[i]<< "states:" <<states[i]<<std::endl;}    }returnrets[n-1];}

Dynamic Programming 04-the longest zigzag sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.