You are viewing a single comment's thread. Return to all comments →
Solution in C++:
void removeCylinder(vector<int>& cylinders, int& top, int& curr, int target) { while(curr > target && top < cylinders.size()) { curr -= cylinders[top]; top++; } } int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3) { int ht1 = std::accumulate(h1.begin(), h1.end(), 0); int ht2 = std::accumulate(h2.begin(), h2.end(), 0); int ht3 = std::accumulate(h3.begin(), h3.end(), 0); vector<int> heights = {ht1, ht2, ht3}; int target = *min_element(heights.begin(), heights.end()); int t1 = 0, t2 = 0, t3 = 0; while(ht1 != ht2 || ht1 != ht3 || ht2 != ht3) { removeCylinder(h1, t1, ht1, target); removeCylinder(h2, t2, ht2, target); removeCylinder(h3, t3, ht3, target); heights = {ht1, ht2, ht3}; target = *min_element(heights.begin(), heights.end()); } return ht1; }
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 →
Solution in C++: