You are viewing a single comment's thread. Return to all comments →
C++ Solution with unlimited input stacks
int equalStacks(std::vector<int> h1, std::vector<int> h2, std::vector<int> h3) { std::multimap<int, std::vector<int>> stacks { }; stacks.insert({ std::accumulate(h1.begin(), h1.end(), 0), h1 }); stacks.insert({ std::accumulate(h2.begin(), h2.end(), 0), h2 }); stacks.insert({ std::accumulate(h3.begin(), h3.end(), 0), h3 }); while (stacks.count(stacks.begin()->first) != stacks.size()) { auto max_sum { stacks.rbegin()->first }; auto max_stacks { stacks.equal_range(max_sum) }; auto max_stacks_copy { std::vector<decltype(stacks)::iterator>() }; for (auto it = max_stacks.first; it != max_stacks.second; ++it) { max_stacks_copy.push_back(it); } for (auto it : max_stacks_copy) { auto node { stacks.extract(it->first) }; node.key() -= *node.mapped().begin(); node.mapped().erase(node.mapped().begin()); stacks.insert(std::move(node)); } } return stacks.begin()->first; }
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 with unlimited input stacks