Skip to content

Instantly share code, notes, and snippets.

@samebchase
Created July 16, 2014 19:27
Show Gist options
  • Save samebchase/cc22a48c98f5d2738613 to your computer and use it in GitHub Desktop.
Save samebchase/cc22a48c98f5d2738613 to your computer and use it in GitHub Desktop.
#include <vector>
#include <cassert>
#include <iostream>
#include <forward_list>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::forward_list;
typedef vector<forward_list<int>::iterator> VecItersToList;
int length_of_shortest_list(vector<forward_list<int>> container) {
int length = container.size();
VecItersToList vector_list_iterators;
for (int idx = 0; idx < length; ++idx) {
vector_list_iterators.push_back(container[idx].begin());
}
int list_length = 1;
while (true) {
for (int idx = 0; idx < length; ++idx) {
if (vector_list_iterators[idx] != container[idx].end()) {
if (++vector_list_iterators[idx] == container[idx].end()) {
return list_length;
}
}
else {
return list_length - 1;
}
}
++list_length;
}
}
int main() {
vector<forward_list<int>> test_array_1 =
{
{1, 2},
{0, 3, 1},
{1, 4, 5, 1},
{1, 2, 3, 4},
{1, 2, 3},
{1, 2, 3, 4, 5, 6},
{4, 6, 1, 3}
};
vector<forward_list<int>> test_array_2 =
{
{1, 2, 3, 4},
{5, 6, 7, 8}
};
vector<forward_list<int>> test_array_3 =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{2, 3, 4},
{1, 2, 3, 4}
};
vector<forward_list<int>> test_array_4 =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{1},
{1, 2, 3, 4}
};
assert(length_of_shortest_list(test_array_1) == 2);
assert(length_of_shortest_list(test_array_2) == 4);
assert(length_of_shortest_list(test_array_3) == 3);
assert(length_of_shortest_list(test_array_4) == 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment