Interviews are considered puzzles.
This commit is contained in:
52
puzzles/interviews/tomtom/tomtom_demo.cpp
Normal file
52
puzzles/interviews/tomtom/tomtom_demo.cpp
Normal file
@@ -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 <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
int solution( std::vector<int> & 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<int> 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;
|
||||
}}
|
||||
|
||||
51
puzzles/interviews/tomtom/tomtom_test1.cpp
Normal file
51
puzzles/interviews/tomtom/tomtom_test1.cpp
Normal file
@@ -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 <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
int solution( std::vector<int> & 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<int> 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;
|
||||
}}
|
||||
|
||||
57
puzzles/interviews/tomtom/tomtom_test2.cpp
Normal file
57
puzzles/interviews/tomtom/tomtom_test2.cpp
Normal file
@@ -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 <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
int solution( std::vector<int> & a )
|
||||
{
|
||||
int max_e = 10001; //
|
||||
std::vector<int> 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<int> 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;
|
||||
}}
|
||||
|
||||
88
puzzles/interviews/tomtom/tomtom_test3.cpp
Normal file
88
puzzles/interviews/tomtom/tomtom_test3.cpp
Normal file
@@ -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 <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;
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user