Guest User

Untitled

a guest
Jul 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<int> advantageCount(vector<int>& A, vector<int>& B) {
  4. int len = A.size();
  5. map<int, int> bst;
  6. for(const auto& num : A)++bst[num];
  7. vector<int> res(len, 0);
  8. for(int i = 0; i < len; ++i)
  9. {
  10. auto iter = bst.upper_bound(B[i]);
  11. if(iter == bst.end())
  12. {
  13. res[i] = bst.begin()->first;
  14. --bst.begin()->second;
  15. if(bst.begin()->second == 0)
  16. bst.erase(bst.begin());
  17. }
  18. else
  19. {
  20. res[i] = iter->first;
  21. --iter->second;
  22. if(iter->second == 0)
  23. bst.erase(iter);
  24. }
  25. }
  26. return res;
  27. }
  28. };
Add Comment
Please, Sign In to add comment