/*
* @lc app=leetcode id=870 lang=cpp
*
* [870] Advantage Shuffle
*/
// @lc code=start
class Solution {
public:
vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
priority_queue<pair<int,int>, vector<pair<int,int>>, less<pair<int,int>>> x,y;
int N = nums1.size();
for(int i=0;i<N;i++){
x.push(make_pair(nums1[i],i));
y.push(make_pair(nums2[i],i));
}
vector<int> ans(N, -1);
while(!y.empty() && !x.empty()){
while(!y.empty() && y.top().first >= x.top().first) y.pop();
if(y.empty()){
break;
}
ans[y.top().second] = x.top().first;
y.pop();
x.pop();
}
int loc = 0;
while(!x.empty()){
while(loc < N && ans[loc] >= 0) loc++;
ans[loc++] = x.top().first;
x.pop();
}
return ans;
}
};
// @lc code=end
No.290 - LeetCode[870] Advantage Shuffle - 优先队列贪心
最新推荐文章于 2026-06-17 20:26:24 发布
该博客介绍了一种利用优先队列解决优势洗牌问题的方法,具体是通过维护两个优先队列,分别存储两个数组的元素,并进行比较和替换,最终返回一个满足条件的数组。代码实现中使用了C++的`<queue>`库,通过`make_pair`创建元素,并使用`less`比较器确保队列始终按值升序排列。
152

被折叠的 条评论
为什么被折叠?



