GastonFontenla

Untitled

Aug 28th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class ZigZag
  6. { public:
  7. int longestZigZag(vector <int> sequence)
  8. {
  9. int n = sequence.size();
  10. vector <int> pos(n, 1), neg(n, 1);
  11.  
  12. for(int i=1; i<n; i++)
  13. {
  14. ///Intento alargar la secuencia que termina en i
  15. for(int j=0; j<i; j++)
  16. {
  17. int dif = sequence[i]-sequence[j];
  18. if(dif < 0)
  19. neg[i] = max(neg[i], pos[j]+1);
  20. if(dif > 0)
  21. pos[i] = max(pos[i], neg[j]+1);
  22. }
  23. }
  24.  
  25. int res = 0;
  26. for(int i=0; i<n; i++)
  27. res = max(res, max(pos[i], neg[i]));
  28. return res;
  29. }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment