Created
March 16, 2018 23:22
-
-
Save damian1996/a4524a1800abf9b81f16d3c8ac2cab56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
using namespace std; | |
int solution(vector<int> &A) { | |
int arrSize = A.size(), prev = A[0], castles = 0; | |
bool check = false; | |
int j = 1; | |
while(true) { | |
if(j<arrSize && A[j-1]==A[j]) { | |
j++; | |
} else if(j<arrSize && A[j-1]!=A[j]) { | |
castles++; | |
break; | |
} else { | |
castles++; | |
break; | |
} | |
} | |
j = arrSize-1; | |
while(true) { | |
if(j>=0 && A[j]==A[j-1]) { | |
j--; | |
} else if(j>0 && A[j]!=A[j-1]) { | |
castles++; | |
break; | |
} else{ | |
break; | |
} | |
} | |
for(int i=1; i<arrSize; i++) { | |
if(A[i]==prev) continue; | |
else if(A[i]>prev) { | |
check = true; | |
} | |
else if(A[i]<prev) { | |
if(check) { | |
castles++; | |
check = false; | |
} | |
} | |
prev = A[i]; | |
} | |
prev = A[0]; | |
check = false; | |
for(int i=1; i<arrSize; i++) { | |
if(A[i]==prev) continue; | |
else if(A[i]<prev) { | |
check = true; | |
} | |
else if(A[i]>prev) { | |
if(check) { | |
castles++; | |
check = false; | |
} | |
} | |
prev = A[i]; | |
} | |
return castles; | |
} | |
int main() { | |
int myInts[] = {2, 2, 3, 4, 3, 3, 2, 2, 1, 1, 2, 5}; | |
vector<int> A(myInts, myInts+sizeof(myInts)/sizeof(int)); | |
//cout << solution(A) << endl; | |
int myInts2[] = {-3, -3}; | |
vector<int> B(myInts2, myInts2+sizeof(myInts2)/sizeof(int)); | |
cout << solution(B) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment