public int longestZigZag(int[] sequence){ if(sequence.length == 1) return 1; int[] L = new int[sequence.length]; int[] diff = new int[sequence.length-1]; int maxLength = 1; boolean maxValue; for(int j =0; j<(sequence.length - 1);j++){ diff[j] = sequence[j+1] - sequence[j]; } if(diff[0] >= 0) maxValue = true; else maxValue = false; for(int j = 1; j< diff.length;j++){ boolean curValue= maxValue; if(diff[j] > 0 ) curValue = true; else if(diff[j]<0) curValue = false; if(curValue != maxValue){ maxLength++; maxValue = curValue; } } return maxLength+1; }
This entry was posted on Monday, December 31st, 2012 at 9:54 am and is filed under programming, TopCoder. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Δ
Enter your email address to follow this blog and receive notifications of new posts by email.
Email Address:
Follow