From ef088b4ab4fdafeef32fa9b65a2c76601112457b Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Sun, 8 Feb 2015 23:05:24 +0400 Subject: [PATCH] tom tom interview. --- interviews/tomtom/tomtom_demo.cpp | 52 ++++++++++++++++++ interviews/tomtom/tomtom_test1.cpp | 51 +++++++++++++++++ interviews/tomtom/tomtom_test2.cpp | 57 +++++++++++++++++++ interviews/tomtom/tomtom_test3.cpp | 88 ++++++++++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 interviews/tomtom/tomtom_demo.cpp create mode 100644 interviews/tomtom/tomtom_test1.cpp create mode 100644 interviews/tomtom/tomtom_test2.cpp create mode 100644 interviews/tomtom/tomtom_test3.cpp diff --git a/interviews/tomtom/tomtom_demo.cpp b/interviews/tomtom/tomtom_demo.cpp new file mode 100644 index 0000000..9cef397 --- /dev/null +++ b/interviews/tomtom/tomtom_demo.cpp @@ -0,0 +1,52 @@ +/* +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include +#include +#include + +int solution( std::vector & a ) +{ + long long s = 0; + for ( auto it = a.begin(), ie=a.end(); it != ie; ++it ) { + s += *it; + } + + long long t = 0; + for ( auto it = a.begin(), ie=a.end(); it != ie; ++it ) { + if ( (s-*it)%2 == 0 && t == (s-*it)/2 ) + return it - a.begin(); + t += *it; + } + return -1; +} + +int main ( void ) +{try{ +#if 1 + std::vector a = { + -1,3,-4,5,1,-6,2,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; +}} + diff --git a/interviews/tomtom/tomtom_test1.cpp b/interviews/tomtom/tomtom_test1.cpp new file mode 100644 index 0000000..3b14b48 --- /dev/null +++ b/interviews/tomtom/tomtom_test1.cpp @@ -0,0 +1,51 @@ +/* +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include +#include +#include + +int solution( std::vector & a ) +{ + auto n = a.size(); + + int i = 0; + for (int c = 0; c < n; ++c ) { + auto new_i = i + a[i]; + if (new_i < 0 || n <= new_i) + return i; + i = new_i; + } + + return -1; +} + +int main ( void ) +{try{ +#if 1 + std::vector a = { + 2,3,-1,1,-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; +}} + diff --git a/interviews/tomtom/tomtom_test2.cpp b/interviews/tomtom/tomtom_test2.cpp new file mode 100644 index 0000000..597a460 --- /dev/null +++ b/interviews/tomtom/tomtom_test2.cpp @@ -0,0 +1,57 @@ +/* +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include +#include +#include + +int solution( std::vector & a ) +{ + int max_e = 10001; // + std::vector c(max_e, 0); + + for ( auto e : a ) { + ++c[e]; + } + + int m = 0; + int mi = 0; + for ( auto it = c.begin(); it != c.end(); ++it ) { + if (*it > m) + { + m = *it; + mi = it - c.begin(); + } + } + return mi; +} + +int main ( void ) +{try{ +#if 1 + std::vector a = { + 0, 0, 1, 2, 0, 10000, 10000, 10000 + }; + + 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; +}} + diff --git a/interviews/tomtom/tomtom_test3.cpp b/interviews/tomtom/tomtom_test3.cpp new file mode 100644 index 0000000..60df41f --- /dev/null +++ b/interviews/tomtom/tomtom_test3.cpp @@ -0,0 +1,88 @@ +/* +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include +#include +#include + +/* +int solution(vector& 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& 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 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; +}} +