You are viewing a single comment's thread. Return to all comments →
No sorting needed when using a counter. Insertion, lookup (using lower_bound and since std::multiset is sorted) and removal is O(logn).
string twoArrays(int k, vector<int> A, vector<int> B) { std::multiset<int> counter{A.begin(), A.end()}; bool ok = true; for (auto x = 0; x < B.size(); ++x) { if (auto it = std::lower_bound(counter.begin(), counter.end(), k-B[x]); it == counter.end()) { ok = false; break; } else { counter.erase(it, std::next(it)); } } return ok ? "YES" : "NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Permuting Two Arrays
You are viewing a single comment's thread. Return to all comments →
No sorting needed when using a counter. Insertion, lookup (using lower_bound and since std::multiset is sorted) and removal is O(logn).