You are viewing a single comment's thread. Return to all comments →
C++ solution:
int getSum(vector<int> h); int equalStacks(vector<int> h1, vector<int> h2, vector<int> h3) { int h1sum = getSum(h1); int h2sum = getSum(h2); int h3sum = getSum(h3); while (h1.size() && h2.size() && h3.size() && !(h1sum == h2sum && h1sum == h3sum)) { auto maxVal = max({ h1sum, h2sum, h3sum }); if (maxVal == h1sum) { h1sum -= h1.at(0); h1.erase(h1.begin()); } else if (maxVal == h2sum) { h2sum -= h2.at(0); h2.erase(h2.begin()); } else { h3sum -= h3.at(0); h3.erase(h3.begin()); } } if (h1.size() && h2.size() && h3.size()) { return h1sum; } else { return 0; } } int getSum(vector<int> h){ return accumulate(h.begin(), h.end(), 0); }
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 →
C++ solution: