fork(1) download
  1. int longestZigZag( vector <int> sequence )
  2. {
  3. int n = sequence.size();
  4. vector <int> dp (n, 1);
  5. vector <int> dif (n, 0);
  6. int temp;
  7. for (int i = 1; i < n; i++)
  8. for (int j = 0; j < i; j++)
  9. {
  10. temp = sequence[i] - sequence[j];
  11. if (temp != 0 && temp*dif[j] <= 0 && dp[j] + 1 > dp[i])
  12. {
  13. dp[i] = dp[j] + 1;
  14. dif[i] = temp;
  15. }
  16. }
  17. sort (dp.begin(), dp.end());
  18. return dp[n-1];
  19. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty