You are viewing a single comment's thread. Return to all comments →
Good logic, here is my implementaion in C++
int max_height(vector<int> h1, vector<int> h2, vector<int> h3) { int n1, n2, n3; n1 = h1.size(); n2 = h2.size(); n3 = h3.size(); reverse(h1.begin(), h1.end()); reverse(h2.begin(), h2.end()); reverse(h3.begin(), h3.end()); vector<int> h1_cumulative(n1 + 1); for (int h1_i = 1; h1_i < n1+1; h1_i++) { h1_cumulative[h1_i] += h1[h1_i-1] + h1_cumulative[h1_i-1]; } vector<int> h2_cumulative(n2 + 1); for (int h2_i = 1; h2_i < n2 + 1; h2_i++) { h2_cumulative[h2_i] += h2[h2_i - 1] + h2_cumulative[h2_i - 1]; } vector<int> h3_cumulative(n3 + 1); for (int h3_i = 1; h3_i < n3 + 1; h3_i++) { h3_cumulative[h3_i] += h3[h3_i - 1] + h3_cumulative[h3_i - 1]; } reverse(h1_cumulative.begin(), h1_cumulative.end()); reverse(h2_cumulative.begin(), h2_cumulative.end()); reverse(h3_cumulative.begin(), h3_cumulative.end()); vector<int>::iterator it1 = h1_cumulative.begin(); vector<int>::iterator it2 = h2_cumulative.begin(); vector<int>::iterator it3 = h3_cumulative.begin(); while (true) { if (*it1 == *it2 && *it2 == *it3) { return *it1; } if (*it1 >= *it2 && *it1 >= *it3) { ++it1; } else if (*it2 >= *it1 && *it2 >= *it3) { ++it2; } else if (*it3 >= *it1 && *it3 >= *it2){ ++it3; } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Equal Stacks
You are viewing a single comment's thread. Return to all comments →
Good logic, here is my implementaion in C++