870. 优势洗牌

代码实现
class Solution {
public:
vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
sort(nums1.begin(), nums1.end(), less<int>());
priority_queue<pair<int, int>,
vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 0; i < n; i++) {
pq.push({nums2[i], i});
}
vector<int> ans(n, -1);
queue<int> res;
int j = 0;
for (int i = 0; i < n && j < n; i++, j++) {
auto [num, pos] = pq.top();
pq.pop();
while (j < n && num >= nums1[j]) {
res.push(nums1[j]);
j++;
}
if (j == n) {
break;
}
ans[pos] = nums1[j];
}
while (j < n) {
res.push(nums1[j]);
j++;
}
for (int i = 0; i < n; i++) {
if (ans[i] == -1) {
ans[i] = res.front();
res.pop();
}
}
return ans;
}
};