/*
* @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 - 优先队列贪心
最新推荐文章于 2024-11-08 23:27:36 发布