Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. int solution(vector<int> &A) {
  2.     /*  If Vector A is empty then return 0  */
  3.     if (A.size() == NULL)
  4.         return 0;
  5.  
  6.     bool status = NULL; //initialize in first loop below
  7.     int i = 1;
  8.     int CriticalPoints = 1; // count of all critical points
  9.     /*  Loop to search second critical point and initialize first (bool)status  */
  10.     for (; i < A.size(); i++)
  11.     {
  12.         if (A[i] > A[i-1])
  13.         {
  14.             status = 1;// True if hill
  15.             CriticalPoints++;
  16.             break;
  17.         }
  18.         if (A[i] < A[i - 1])
  19.         {
  20.             status = 0;//False if valley
  21.             CriticalPoints++;
  22.             break;
  23.         }
  24.     }
  25.     /*  If first loop end without break and 'i' is equal size of vector A, then
  26.     all items in this vector have this same value, so return CriticalPoints (== 1)  */
  27.     if (i == A.size())
  28.         return CriticalPoints;
  29.     /*  Else, search all critical points in this vector and return their count  */
  30.     for (; i < A.size(); i++)
  31.     {
  32.         if ((status && A[i] < A[i - 1]) || (!status && A[i] > A[i - 1]))
  33.         {
  34.             CriticalPoints++;
  35.             status = !status;
  36.         }
  37.     }
  38.  
  39.     return CriticalPoints;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement