Files
test/puzzles/interviews/tomtom/tomtom_test3.cpp

89 lines
1.3 KiB
C++

/*
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
VIM: let g:argv=""
*/
#include <iostream>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <string>
/*
int solution(vector<int>& A) {
int n = A.size();
int i = n - 1;
int result = -1;
int k = 0;
int maximal = 0;
while (i > 0) {
if (A[i] == 1) {
k = k + 1;
if (k >= maximal) {
maximal = k;
result = i;
}
}
else {
k = 0;
}
i = i - 1;
}
if (A[i] == 1 && k + 1 > maximal)
result = 0;
return result;
}
*/
using namespace std;
int solution(vector<int>& A) {
int n = A.size();
int i = n - 1;
int result = -1;
int k = 0;
int maximal = 0;
while (i > 0) {
if (A[i] == 1) {
k = k + 1;
if (k >= maximal) {
maximal = k;
result = i;
}
}
else {
k = 0;
}
i = i - 1;
}
if (A[i] == 1 && k + 1 >= maximal)
result = 0;
return result;
}
int main ( void )
{try{
#if 1
std::vector<int> a = {
1,1,1,0,1,1,1,0,1
};
std::cout << solution( a ) << std::endl;
#endif
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}