Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> advantageCount(vector<int>& A, vector<int>& B) {
- int len = A.size();
- map<int, int> bst;
- for(const auto& num : A)++bst[num];
- vector<int> res(len, 0);
- for(int i = 0; i < len; ++i)
- {
- auto iter = bst.upper_bound(B[i]);
- if(iter == bst.end())
- {
- res[i] = bst.begin()->first;
- --bst.begin()->second;
- if(bst.begin()->second == 0)
- bst.erase(bst.begin());
- }
- else
- {
- res[i] = iter->first;
- --iter->second;
- if(iter->second == 0)
- bst.erase(iter);
- }
- }
- return res;
- }
- };
Add Comment
Please, Sign In to add comment